Solve Multiple Coding Interview Problems
Company: Pinterest
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This multi-part question evaluates skills in graph traversal and shortest-path reasoning (minimum transit routes), precise string and numeric manipulation with carry handling (rounding), financial accounting and minimization of transactions (shared expenses), and scheduling with ordered data structures and nearest-neighbor selection (dynamic elevator assignment). Commonly asked in the Coding & Algorithms domain to assess algorithmic reasoning, robust edge-case handling, data-structure selection, and time/space complexity analysis, it combines conceptual understanding of algorithms with practical implementation concerns for efficient, correct solutions.
Part 1: Minimum Transit Routes
Constraints
- 0 <= len(lines) <= 10000
- 1 <= total number of stop entries across all lines <= 100000
- Stops are integers
- If source == target, the answer is 0
Examples
Input: ([[1, 2, 7], [3, 6, 7]], 1, 6)
Expected Output: 2
Explanation: Take line 0 from stop 1 to stop 7, then transfer to line 1 to reach stop 6.
Input: ([[1, 5, 7], [3, 6, 9]], 1, 6)
Expected Output: -1
Explanation: The two lines do not share a stop, so the destination cannot be reached.
Input: ([[1, 2, 3]], 2, 2)
Expected Output: 0
Explanation: Source and target are the same stop, so no line is needed.
Input: ([], 1, 2)
Expected Output: -1
Explanation: There are no transit lines at all.
Hints
- Build a mapping from each stop to all line indices that contain that stop.
- Run BFS by line count: boarding a new line increases the answer by 1.
Part 2: Round by the Least Significant Digit
Constraints
- 1 <= len(num) <= 100000
- num contains only digits and at most one decimal point
- num has no sign
- There is at least one digit in the input
Examples
Input: ('1295',)
Expected Output: '1300'
Explanation: The last digit is 5, so carry into the tens place: 1295 becomes 1300.
Input: ('1.235',)
Expected Output: '1.24'
Explanation: The last digit is 5, so the hundredths digit increases from 3 to 4.
Input: ('1.99',)
Expected Output: '2.0'
Explanation: The last digit is 9, so carrying crosses the decimal boundary.
Input: ('9',)
Expected Output: '10'
Explanation: A carry across all integer digits creates a new leading digit.
Input: ('0.04',)
Expected Output: '0.0'
Explanation: The last digit is 4, so it rounds down while keeping one fewer fractional digit.
Hints
- Temporarily remove the decimal point and treat the number like a digit string.
- After processing the carry, put the decimal point back so the result has one fewer fractional digit than before.
Part 3: Simplify Shared Expenses
Constraints
- 1 <= n <= 8
- 0 <= len(expenses) <= 50
- All amounts and shares are non-negative integers in cents
- For equal splits, amount is divisible by the number of participants
- For exact splits, the provided shares sum exactly to amount
- The exact optimization is intended for small n
Examples
Input: (3, [[0, 3000, 0, 3, 0, 1, 2]])
Expected Output: [[1, 0, 1000], [2, 0, 1000]]
Explanation: User 0 paid for all three users equally, so users 1 and 2 each owe user 0 one share.
Input: (4, [[0, 2000, 0, 2, 0, 1], [2, 2000, 0, 2, 2, 3]])
Expected Output: [[1, 0, 1000], [3, 2, 1000]]
Explanation: The two expenses are independent, so one payment settles each pair.
Input: (4, [[0, 4000, 0, 4, 0, 1, 2, 3], [2, 1000, 1, 2, 0, 2, 200, 800]])
Expected Output: [[1, 0, 1000], [2, 0, 800], [3, 0, 1000]]
Explanation: After combining both expenses, user 0 should receive 2800 cents and users 1, 2, and 3 owe 1000, 800, and 1000 cents.
Input: (1, [[0, 500, 0, 1, 0]])
Expected Output: []
Explanation: The only user paid only for themselves, so the net balance is already zero.
Input: (3, [])
Expected Output: []
Explanation: No expenses means no balances to settle.
Hints
- First compress all expenses into one net balance per user.
- After that, the problem becomes an exact minimum-transaction settlement search over a small state space.
Part 4: Dynamic Elevator Assignment
Constraints
- 1 <= len(elevators) <= 2000
- 0 <= len(requests) <= 2000
- Elevator ids are unique integers
- Timestamps are non-decreasing
- Floors are integers
Examples
Input: ([[1, 0], [2, 10]], [[0, 2], [3, 9], [5, 1]])
Expected Output: [1, 2, 1]
Explanation: Elevator 1 handles the first and third requests, and elevator 2 is closer to the second.
Input: ([[1, 0], [2, 4]], [[1, 3], [1, 1]])
Expected Output: [2, 1]
Explanation: The second request happens at the same timestamp, so the previously chosen elevator has not moved yet.
Input: ([[5, 2], [3, 4]], [[0, 3]])
Expected Output: [3]
Explanation: Both elevators are equally close to floor 3, so the smaller id wins.
Input: ([[7, 0]], [[0, 10], [3, 2], [5, 5]])
Expected Output: [7, 7, 7]
Explanation: The single elevator is reassigned while moving; each new target starts from its current floor at that time.
Hints
- Store each elevator's current floor, current target, and last update time; update lazily when a request arrives.
- After all elevators are updated to the request time, choose the one minimizing distance to pickup, then id.