Answer multi-round grid and data-structure questions
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This multi-part question evaluates algorithmic problem-solving and data structure design skills across ordered-array search with dynamic updates, expected O(1) randomized set operations, nesting-depth string parsing, Manhattan-range counting on grids, and constrained shortest-path search.
Find First and Last Position in Sorted Array
Constraints
- 0 <= n <= 2 * 10^5
- Values fit in a 32-bit signed integer
- nums is sorted in non-decreasing order
Examples
Input: ([5,7,7,8,8,10], 8)
Expected Output: [3, 4]
Explanation: 8 first appears at index 3 and last at index 4.
Input: ([5,7,7,8,8,10], 6)
Expected Output: [-1, -1]
Explanation: 6 does not appear in the array.
Input: ([], 0)
Expected Output: [-1, -1]
Explanation: Empty array: target cannot be found.
Input: ([1], 1)
Expected Output: [0, 0]
Explanation: Single element equal to target.
Input: ([2,2,2,2,2], 2)
Expected Output: [0, 4]
Explanation: All elements equal target; range spans the whole array.
Input: ([-3,-1,-1,0,4], -1)
Expected Output: [1, 2]
Explanation: Handles negative values and duplicates.
Hints
- Run one binary search for the leftmost index where target could be inserted (lower_bound).
- If that index is out of range or the value there is not target, the target is absent.
- Run a second binary search for the rightmost position (upper_bound) and subtract 1 to get the last index.
O(1) Lottery Participant Store
Constraints
- ids are unique integers
- Each operation is O(1) expected
- randomPick returns -1 when there are no participants
Examples
Input: ([['add', 1], ['add', 2], ['add', 1], ['remove', 1], ['remove', 5]],)
Expected Output: [True, True, False, True, False]
Explanation: Adding 1 again fails; removing 1 succeeds; removing absent 5 fails.
Input: ([['pick']],)
Expected Output: [-1]
Explanation: Picking from an empty store returns -1.
Input: ([['add', 7], ['pick'], ['remove', 7], ['pick']],)
Expected Output: [True, 7, True, -1]
Explanation: With one participant the pick is deterministically 7; after removal the store is empty.
Input: ([['add', 3], ['add', 4], ['remove', 3], ['add', 3], ['remove', 4]],)
Expected Output: [True, True, True, True, True]
Explanation: Remove then re-add 3 works because the index map is kept consistent via swap-with-last.
Input: ([['remove', 99], ['add', 99], ['remove', 99], ['remove', 99]],)
Expected Output: [False, True, True, False]
Explanation: Remove before add fails; the second removal of 99 fails since it is gone.
Hints
- Keep a dynamic array of ids and a hash map from id to its index in that array.
- To remove in O(1), swap the target with the last element, pop the last, and update the moved element's stored index.
- randomPick is just a uniform random array index; return -1 when the array is empty.
Characters at Maximum Parenthesis Depth
Constraints
- |s| <= 2 * 10^5
- s contains only lowercase letters and '(' ')'
- Parentheses are balanced
Examples
Input: ('a(b(c)d)e',)
Expected Output: c
Explanation: Max depth 2 is reached only around 'c'.
Input: ('((ab)(cd))',)
Expected Output: abcd
Explanation: Both 'ab' and 'cd' sit at depth 2, the maximum.
Input: ('abc',)
Expected Output: abc
Explanation: No parentheses: max depth is 0, so every letter qualifies.
Input: ('',)
Expected Output:
Explanation: Empty string yields empty output.
Input: ('(())',)
Expected Output:
Explanation: Max depth 2 but no letters anywhere, so output is empty.
Input: ('x(y)(z(w))',)
Expected Output: w
Explanation: Max depth 2 is only reached at 'w'.
Hints
- Track a running depth: +1 on '(', -1 on ')'.
- First pass: record the maximum depth ever reached.
- Second pass: append a letter only when the current depth equals that maximum. With no parentheses the max depth is 0, so all letters qualify.
Place One Turret to Protect the Most Houses
Constraints
- 1 <= m, n <= 2000 (discuss scalable approaches)
- 0 <= k <= 2000
- Each cell is 0 or 1
- The turret may sit on a house or an empty cell
Examples
Input: ([[1,0,1],[0,0,0],[1,0,1]], 1)
Expected Output: 2
Explanation: Placing the turret on the middle of the top edge (0,1) covers the two top-corner houses at distance 1.
Input: ([[1,0,1],[0,0,0],[1,0,1]], 2)
Expected Output: 4
Explanation: The center (1,1) is distance 2 from all four corner houses.
Input: ([[0,0],[0,0]], 5)
Expected Output: 0
Explanation: No houses to protect.
Input: ([[1,1,1]], 0)
Expected Output: 1
Explanation: With k=0 a turret protects only the house on its own cell.
Input: ([[1,1],[1,1]], 100)
Expected Output: 4
Explanation: A large radius covers every house from any cell.
Input: ([[1,0,0,1]], 2)
Expected Output: 2
Explanation: Placing at index 1 covers the house at index 0 (dist 1) and index 3 (dist 2).
Hints
- Collect the coordinates of all houses once.
- For each candidate turret cell, count houses with Manhattan distance <= k.
- To scale, rotate coordinates to (r+c, r-c); a Manhattan ball becomes an axis-aligned square, enabling an O(1) 2D prefix-sum query per candidate.
Shortest Path in a Grid with Up to k Wall Breaks
Constraints
- 1 <= m, n <= 100
- 0 <= k <= m * n
- Cells are 0 (empty) or 1 (wall)
- Movement is 4-directional
Examples
Input: ([[0,0,0],[1,1,0],[0,0,0]], 0)
Expected Output: 4
Explanation: A clear path of length 4 exists without breaking any wall.
Input: ([[0,1,0,1,0]], 1)
Expected Output: -1
Explanation: Two walls block the single row; only 1 break is allowed.
Input: ([[0,1,0,1,0]], 2)
Expected Output: 4
Explanation: Breaking both walls gives a straight path of length 4.
Input: ([[0]], 0)
Expected Output: 0
Explanation: Start equals destination; zero steps.
Input: ([[0,0],[1,0]], 0)
Expected Output: 2
Explanation: Go right then down avoiding the wall, length 2.
Input: ([[0,1],[1,0]], 5)
Expected Output: 2
Explanation: Either route breaks exactly one wall; with k=5 the shortest is length 2.
Hints
- BFS guarantees the shortest path in an unweighted grid; augment the state with the number of walls already broken.
- For each cell store the minimum walls used to reach it; skip a neighbor if you cannot improve on it (and never exceed k).
- Return the distance the moment you first reach the bottom-right cell; return -1 if BFS drains without reaching it.