Three Array Problems for Trading Geometry and Grouping
Company: Zoox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem
Implement `solve(prices, numbers, people, group_size)` and return an object with three fields.
1. `max_profit`: the maximum profit from buying once and selling once later in `prices`. Return `0` when no profitable trade exists.
2. `line_witness`: choose four values from four distinct indices of `numbers` and assign them to `(x, y, k, b)` so that `y = k*x + b`. Return the lexicographically smallest valid `[x, y, k, b]`, or `null` if none exists.
3. `groups`: partition all people into groups of exactly `group_size` so that no group contains two people with the same nickname. Each person is `{name, nickname}`. The input is guaranteed to have a feasible partition and its length is divisible by `group_size`. Return each group as an increasing list of zero-based original person indices, and sort the outer list lexicographically. Among all valid normalized partitions, return the lexicographically smallest outer list. This makes the result unique.
## Constraints
- `1 <= len(prices) <= 200,000`
- `1 <= len(numbers) <= 60`; values are integers in `[-10^6, 10^6]`
- `1 <= len(people) <= 18`
- People have unique names and nonempty nicknames.
- `1 <= group_size <= len(people)`
## Examples
For `prices = [7, 1, 5, 3, 6, 4]`, `max_profit` is `5`.
For `numbers = [1, 3, 2, 1]`, one witness is `[1, 3, 2, 1]` because `3 = 2*1 + 1` and all four positions are distinct.
For nicknames `[a, a, b, b]` and `group_size = 2`, `groups` is `[[0, 2], [1, 3]]`. The other normalized partition, `[[0, 3], [1, 2]]`, is valid but lexicographically larger.
## Clarifications
The three parts are independent. A person must appear exactly once. Lexicographic comparison of groupings compares the first differing integer in the first differing group. A trade must sell after it buys. Integer multiplication for the line equation should use a wide enough type to avoid overflow.
## Hint
Use a one-pass invariant for the trade. For the line witness, pre-index values while respecting distinct indices. For grouping, distribute repeated nicknames across different groups before filling remaining slots.
## Interview Follow-ups
- Detect and report infeasible nickname inputs.
- Reduce the memory used by the line search.
- Extend the trading part to at most two transactions.
Quick Answer: Solve three independent array and grouping tasks: one buy/sell profit, a four-value line witness using distinct indices, and deterministic nickname-safe grouping. Respect large inputs, wide integer arithmetic, unique normalized output, no-profit and no-witness cases, feasibility checks, and follow-up extensions.