Solve five hard algorithm problems
Company: Pinterest
Role: Machine Learning Engineer
Category: Coding & Algorithms
Interview Round: Onsite
Quick Answer: This set of problems evaluates algorithm design and problem-solving skills across array manipulation, range-update techniques, load-balancing and partitioning strategies, and combinatorial packing, testing competencies in complexity analysis, data-structure use, and optimization under constraints.
Part 1: Minimum Range +/-1 Operations to Match Target
Constraints
- 0 <= n <= 2 * 10^5
- -10^9 <= nums[i], target[i] <= 10^9
- len(nums) == len(target)
- The answer can be larger than 32-bit integer range
Examples
Input: ([3, 5, 1, 2], [4, 6, 2, 4])
Expected Output:
Explanation: The needed difference is [1, 1, 1, 2]. One +1 operation on [0..3] and one +1 operation on [3..3] are enough.
Input: ([1, 3, 2], [2, 1, 3])
Expected Output:
Explanation: The difference is [1, -2, 1]. Positive and negative requirements must be handled separately, giving 4 total operations.
Input: ([5, 5], [2, 3])
Expected Output:
Explanation: The difference is [-3, -2]. Subtract 1 on [0..1] twice, then subtract 1 on [0..0] once.
Input: ([], [])
Expected Output:
Explanation: Empty arrays already match.
Hints
- Let diff[i] = target[i] - nums[i]. A +1 interval only contributes to positive values of diff, and a -1 interval only contributes to negative values.
- For the positive part and the negative part separately, count how much the required height increases as you scan from left to right.
Part 2: Build a Target Array from Zeros Using Range Increments
Constraints
- 0 <= n <= 2 * 10^5
- 0 <= target[i] <= 10^9
- The answer can be larger than 32-bit integer range
Examples
Input: ([1, 2, 3, 2, 1],)
Expected Output:
Explanation: You need 1 layer starting at index 0, another starting at index 1, and another starting at index 2.
Input: ([3, 1, 1, 2],)
Expected Output:
Explanation: Build 3 units at index 0, then only 1 new operation is needed when the height rises from 1 to 2 at the end.
Input: ([5],)
Expected Output:
Explanation: A single element of height 5 needs 5 operations.
Input: ([0, 0, 0],)
Expected Output:
Explanation: The zero array already matches the target.
Input: ([],)
Expected Output:
Explanation: Empty target needs no operations.
Hints
- Think of target as a skyline or histogram. Each operation paints one horizontal strip over a contiguous segment.
- A new operation must start at index i exactly when target[i] is greater than target[i - 1].
Part 3: Minimize the Maximum Load When Assigning Jobs
Constraints
- 0 <= len(jobs) <= 12
- 1 <= k <= 12
- 0 <= jobs[i] <= 10^7
- Each job must be assigned to exactly one worker
- Workers may receive zero jobs
Examples
Input: ([3, 2, 3], 3)
Expected Output:
Explanation: Each worker can take one job, so the maximum load is 3.
Input: ([1, 2, 4, 7, 8], 2)
Expected Output:
Explanation: One optimal assignment is [8, 2, 1] and [7, 4], so the maximum load is 11.
Input: ([5, 5, 4, 4, 4], 2)
Expected Output:
Explanation: Best possible is 12, for example [5, 5] and [4, 4, 4].
Input: ([10], 5)
Expected Output:
Explanation: There is only one job, so the answer is 10.
Input: ([], 3)
Expected Output:
Explanation: No jobs means no load.
Hints
- The answer is between max(jobs) and sum(jobs). Try binary searching that limit.
- To check whether a limit works, assign larger jobs first and prune symmetric states where two workers currently have the same load.
Part 4: Place Boxes into a Warehouse from the Left Side
Constraints
- 0 <= len(boxes), len(warehouse) <= 10^5
- 1 <= boxes[i], warehouse[i] <= 10^9
- Boxes may be reordered arbitrarily
- Each room can contain at most one box
Examples
Input: ([4, 3, 4, 1], [5, 3, 3, 4, 1])
Expected Output:
Explanation: After prefix-min preprocessing, the usable room heights become [5, 3, 3, 3, 1]. Three boxes can be placed.
Input: ([1, 2, 2, 3, 4], [3, 4, 1, 2])
Expected Output:
Explanation: The effective room heights are [3, 3, 1, 1]. Only three boxes can be placed.
Input: ([1, 2, 3], [3, 3, 3])
Expected Output:
Explanation: All three boxes fit because every room has effective height 3.
Input: ([5], [4])
Expected Output:
Explanation: The box is too tall for the only room.
Input: ([], [1, 2, 3])
Expected Output:
Explanation: No boxes are available.
Hints
- A room's true usable height is limited by the smallest room before it, because every box must pass through that prefix from the left.
- After preprocessing those effective heights, put the smallest boxes into the deepest available rooms first.
Part 5: Place Boxes into a Warehouse from Either Side
Constraints
- 0 <= len(boxes), len(warehouse) <= 10^5
- 1 <= boxes[i], warehouse[i] <= 10^9
- Boxes may be reordered arbitrarily
- Each room can contain at most one box
Examples
Input: ([1, 2, 2, 3, 4], [3, 4, 1, 2])
Expected Output:
Explanation: Using both entrances, the effective room capacities are [3, 3, 1, 2]. Four boxes can be matched.
Input: ([4, 4, 4], [5, 1, 5])
Expected Output:
Explanation: One box can go into the left room from the left and one into the right room from the right.
Input: ([2, 3, 5, 5], [2, 1, 3, 4, 5])
Expected Output:
Explanation: The best split is around the smallest room, giving capacities that fit three boxes.
Input: ([1], [1])
Expected Output:
Explanation: The only box fits in the only room.
Input: ([], [2, 3])
Expected Output:
Explanation: No boxes are available.
Hints
- There is an optimal arrangement with a split point: rooms on one side are effectively filled from the left, and rooms on the other side from the right.
- Compute each room's reachable height from the left and from the right, keep the better one, then sort those capacities and greedily match boxes.