ebay interview coding question
Summary
I recently went through an eBay coding interview experience, which consisted of two distinct rounds focused on algorithmic problem-solving related to arrays and binary trees.
Full Experience
Coding:
r1: Array problem — Find the maximum “balanced sum” subarray Idea: A “balanced sum” is defined as a subarray where the sum of the first half equals the sum of the second half. First, preprocess a prefix sum array. Then iterate over all possible subarray midpoints and use the prefix sums to efficiently compute the sums of the left and right halves. Track the maximum length (or sum) that satisfies the condition. Note that the subarray length must be even. Follow-up: If one “adjustable element” is allowed within the subarray, how would you find the subarray with the maximum balanced sum?
r2: Binary tree problem — Count the number of subtrees where the height difference between left and right subtrees is at most 1 and the total sum of node values is even Idea: Use postorder traversal of the binary tree. For each subtree, compute and return its height and the sum of its node values. During traversal, check whether the subtree satisfies both conditions and increment the counter accordingly. Be careful with the initialization rules for height and sum of null nodes. Follow-up: If the subtree is required to be a “complete binary tree,” how would you modify the validation logic?
Interview Questions (2)
Find Maximum Balanced Sum Subarray
Find the maximum “balanced sum” subarray where a “balanced sum” is defined as a subarray where the sum of the first half equals the sum of the second half. Note that the subarray length must be even.
Follow-up: If one “adjustable element” is allowed within the subarray, how would you find the subarray with the maximum balanced sum?
Count Subtrees with Specific Properties
Count the number of subtrees in a binary tree where the height difference between left and right subtrees is at most 1 AND the total sum of node values is even.
Follow-up: If the subtree is required to be a “complete binary tree,” how would you modify the validation logic?
Preparation Tips
I referred to programhelp.net for eBay interview experiences and OA discussions.