Implement multiple coding round problems
Company: Instacart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This multi-part question evaluates algorithmic problem-solving skills including efficient lookup/search, stable ordering and priority management, streaming algorithms under strict memory constraints, and expression parsing with variable substitution.
Part 1: Bus Schedule - Next Departure
Constraints
- 0 <= len(buses), len(arrivals) <= 2 * 10^5
- `buses` is sorted in nondecreasing order
- All times fit in 32-bit signed integers
Examples
Input: ([5, 10, 20], [0, 5, 6, 20, 21])
Expected Output: [5, 5, 10, 20, -1]
Explanation: Each arrival is matched to the first bus at or after that time.
Input: ([7], [1, 7, 8])
Expected Output: [7, 7, -1]
Explanation: A single bus can satisfy multiple passengers if its departure time is late enough.
Input: ([3, 9], [])
Expected Output: []
Explanation: Edge case: no passengers.
Input: ([], [1, 2, 3])
Expected Output: [-1, -1, -1]
Explanation: Edge case: no buses are available.
Hints
- For one passenger, this is a search for the first bus time that is not smaller than the arrival time.
- A binary search per passenger is fast enough for the given limits.
Part 2: Priority Boarding Order
Constraints
- 0 <= n <= 2 * 10^5, where n is the number of passengers
- `len(priorities) == len(check_in_times) == len(names)`
- `priorities[i]` and `check_in_times[i]` are integers
- `names[i]` is a string
Examples
Input: ([1, 0, 1], [5, 10, 3], ["Ann", "Bob", "Cara"])
Expected Output: ["Bob", "Cara", "Ann"]
Explanation: Bob has the best priority. Between Cara and Ann, Cara checked in earlier.
Input: ([1, 1, 1], [5, 5, 5], ["A", "B", "C"])
Expected Output: ["A", "B", "C"]
Explanation: All keys tie, so input order is preserved.
Input: ([2, 1, 1, 0], [1, 9, 3, 8], ["D", "E", "F", "G"])
Expected Output: ["G", "F", "E", "D"]
Explanation: Priority 0 boards first, then priority 1 sorted by check-in time, then priority 2.
Input: ([], [], [])
Expected Output: []
Explanation: Edge case: no passengers.
Hints
- If two passengers are completely tied on the first two rules, their original index decides the order.
- Sorting indices instead of modifying the original arrays can make tie-breaking easier.
Part 3: Streaming Priority Boarding
Constraints
- 0 <= q <= 2 * 10^5, where q is the number of events
- `len(op_types) == len(priorities) == len(check_in_times) == len(names)`
- `op_types[i]` is either `arrive` or `board`
- Each `arrive` or `board` operation should be handled efficiently
Examples
Input: (["arrive", "arrive", "board", "board"], [1, 0, 0, 0], [5, 10, 0, 0], ["Ann", "Bob", "", ""])
Expected Output: ["Bob", "Ann"]
Explanation: Bob has higher priority than Ann, so he boards first.
Input: (["arrive", "arrive", "board", "board"], [1, 1, 0, 0], [5, 5, 0, 0], ["Ann", "Ben", "", ""])
Expected Output: ["Ann", "Ben"]
Explanation: The two passengers tie on priority and check-in time, so arrival order decides it.
Input: (["board", "arrive", "board", "board"], [0, 2, 0, 0], [0, 7, 0, 0], ["", "Cara", "", ""])
Expected Output: ["", "Cara", ""]
Explanation: Edge case: boarding from an empty waiting line returns an empty string.
Input: (["arrive", "arrive", "board", "arrive", "board", "board"], [2, 1, 0, 1, 0, 0], [4, 9, 0, 3, 0, 0], ["Dan", "Eve", "", "Finn", "", ""])
Expected Output: ["Eve", "Finn", "Dan"]
Explanation: After Eve boards, Finn arrives with the same priority level but an earlier check-in time than Eve had.
Hints
- You need a data structure that can always give you the smallest item by `(priority, check_in_time, arrival_sequence)`.
- A min-heap is a natural fit for repeatedly taking the next passenger to board.
Part 4: Decode Passwords from a Stream
Constraints
- 0 <= n <= 2 * 10^5, where n is the number of pairs
- `len(indices) == len(chars)`
- The first password is valid and determines the common length `L`
- All later pairs follow the same password format
Examples
Input: ([0, 1, 2, 2, 0, 1], ["a", "b", "c", "x", "y", "z"])
Expected Output: ["abc", "yzx"]
Explanation: The repeated index `2` starts the second password.
Input: ([2, 0, 1], ["c", "a", "b"])
Expected Output: ["abc"]
Explanation: Only one password appears in the stream.
Input: ([0, 0, 0], ["a", "b", "c"])
Expected Output: ["a", "b", "c"]
Explanation: Edge case: password length is 1, so every pair starts a new password after the first.
Input: ([], [])
Expected Output: []
Explanation: Edge case: empty stream.
Hints
- The first repeated index tells you that the first password is complete and also reveals its length.
- Once `L` is known, a fixed-size buffer and a seen-array are enough to decode the rest.
Part 5: Evaluate an Arithmetic Expression with Variables
Constraints
- 1 <= len(expr) <= 2 * 10^5
- The expression is valid
- Tokens may appear without spaces between them
- `values` contains every variable used in `expr`
Examples
Input: ("1 + x - (y - 2)", {"x": 3, "y": 10})
Expected Output: -4
Explanation: This evaluates to `1 + 3 - (10 - 2) = -4`.
Input: ("(a-(b-5))+10", {"a": 7, "b": 2})
Expected Output: 20
Explanation: Nested parentheses and variables are both used.
Input: ("-x+5", {"x": 8})
Expected Output: -3
Explanation: Edge case: the expression starts with a unary minus.
Input: (" 42 ", {})
Expected Output: 42
Explanation: Edge case: a single number with whitespace.
Input: ("x-(y-(x-1))", {"x": 2, "y": 5})
Expected Output: -2
Explanation: No spaces are required between tokens.
Hints
- This is a parentheses parsing problem; a stack can remember the result and sign before each `(`.
- Parse multi-digit numbers and multi-letter variable names as whole tokens, not one character at a time.