Solve linked list, grid BFS, and median queries
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This multi-part question evaluates proficiency in in-place linked list manipulation, reasoning about shortest connections between connected components in grid graphs, and selection under limited-information rank-count queries, testing algorithmic design, complexity analysis, and data-structure handling in the Coding & Algorithms domain.
Part 1: Reorder a Linked List by Odd and Even Positions
Constraints
- 0 <= len(values) <= 100000
- -10^9 <= values[i] <= 10^9
- The intended linked-list algorithm must run in O(n) time.
- The intended linked-list algorithm must use O(1) extra space, excluding input/output conversion.
Examples
Input: ([1, 2, 3, 4, 5],)
Expected Output: [1, 3, 5, 2, 4]
Explanation: Odd positions are 1, 3, 5; even positions are 2, 4.
Input: ([],)
Expected Output: []
Explanation: An empty list remains empty.
Input: ([1],)
Expected Output: [1]
Explanation: A single-node list has only an odd-position node.
Input: ([1, 2],)
Expected Output: [1, 2]
Explanation: The odd group is [1] and the even group is [2].
Input: ([2, 1, 3, 5, 6, 4, 7],)
Expected Output: [2, 3, 6, 7, 1, 5, 4]
Explanation: Odd-position values are [2, 3, 6, 7], followed by even-position values [1, 5, 4].
Hints
- Maintain two chains: one for odd-position nodes and one for even-position nodes.
- Keep a pointer to the head of the even chain so you can attach it after the odd chain at the end.
Part 2: Shortest Distance Between Any Two Islands
Constraints
- 1 <= len(grid) <= 1000
- 1 <= len(grid[0]) <= 1000
- grid[r][c] is either 0 or 1
- There are at least two islands in the grid.
- Connectivity is 4-directional: up, down, left, and right.
Examples
Input: ([[1, 0, 1]],)
Expected Output: 1
Explanation: The single water cell between the two islands must be flipped.
Input: ([[1, 0], [0, 1]],)
Expected Output: 1
Explanation: The two diagonal land cells are separate islands; flipping either adjacent water cell connects them.
Input: ([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 1, 1], [0, 0, 0, 1, 1]],)
Expected Output: 2
Explanation: The closest path between the two islands crosses two water cells.
Input: ([[1, 0, 1], [0, 0, 0], [1, 0, 0]],)
Expected Output: 1
Explanation: The two islands in the top row can be connected by flipping the water cell between them.
Input: ([[1, 0, 0], [0, 0, 0], [0, 0, 1]],)
Expected Output: 3
Explanation: The Manhattan distance between the two land cells is 4, so 3 water cells must be flipped.
Hints
- First label each island with a unique id.
- After labeling, run a multi-source BFS starting from all land cells at once. When BFS frontiers from two different islands meet, you have a candidate answer.
Part 3: Find the Median Using a Rank-Count Oracle
Constraints
- 1 <= len(values) <= 100000
- lower_bound <= values[i] <= upper_bound for every i
- -10^9 <= lower_bound <= upper_bound <= 10^9
- The intended algorithm should make O(log(upper_bound - lower_bound + 1)) oracle queries.
- Duplicate values are allowed.
Examples
Input: ([7], 0, 10)
Expected Output: 7
Explanation: With one value, that value is the median.
Input: ([5, 1, 9, 3, 7], 0, 10)
Expected Output: 5
Explanation: Sorted order is [1, 3, 5, 7, 9], so the median is 5.
Input: ([10, 2, 8, 4], 0, 10)
Expected Output: 4
Explanation: Sorted order is [2, 4, 8, 10], so the lower median is the 2nd smallest value, 4.
Input: ([-5, -1, -5, 10, 3, 3], -10, 10)
Expected Output: -1
Explanation: Sorted order is [-5, -5, -1, 3, 3, 10], so the lower median is the 3rd smallest value, -1.
Input: ([4, 4, 4, 4], 0, 10)
Expected Output: 4
Explanation: All values are equal, so the median is 4.
Hints
- For a candidate x, the number of values less than or equal to x is N - G(x).
- Binary search for the smallest x whose less-than-or-equal count reaches the target median rank.