Solve linked list, grid BFS, and median queries
Algorithmic Tasks
You are presented with three independent algorithmic tasks.
Task 1: Reorder a Linked List by Odd/Even Positions
Given the head of a singly linked list, reorder the nodes such that all nodes at odd indices come first, followed by all nodes at even indices (1-indexed positions). Within both the odd and even groups, the original relative order of nodes must be preserved.
- Input:
headof a singly linked list - Output: The head of the reordered list
- Constraints: O(1) extra space (excluding recursion stack space), O(n) time complexity.
Example: 1 -> 2 -> 3 -> 4 -> 5 becomes 1 -> 3 -> 5 -> 2 -> 4.
Task 2: Shortest Distance Between Any Two Islands (Grid Variant)
You are given an m x n grid composed of 0s and 1s, where 1 represents land and 0 represents water. An island is defined as a 4-directionally connected component of 1s.
You are allowed to convert water cells (0) to land cells (1). The "distance" between two islands is the minimum number of 0 cells that must be converted to create a 4-directional land path connecting them.
Your task is to return the minimum distance over all pairs of distinct islands.
- Input:
grid[m][n]of0/1 - Output: An integer representing the minimum number of flips
- Assumptions/Constraints:
m, ncan be up to approximately10^3. Design an efficient approach.- There are guaranteed to be at least two distinct islands in the grid.
Task 3: Find the Median Using a Rank-Count Oracle
There are N unknown integers, and you do not have direct access to the array containing them. You are provided with:
- The total count
Nof these integers. - An oracle function
countLessGreater(x)that, when called with an integerx, returns two integers:L(x): The count of numbers strictly less thanx.G(x): The count of numbers strictly greater thanx.
Using only calls to this oracle, find a median value.
- If
Nis odd, return the valuemsuch that exactly(N-1)/2numbers are less thanmand(N-1)/2numbers are greater thanm. - If
Nis even, return the lower median (defined as theN/2-th smallest value, using 1-indexed ranking).
State any additional assumptions you need (e.g., a known bounded integer range), and design an algorithm that minimizes the number of oracle queries.
AI interview prep
Core dilemma
Demonstrating comprehensive algorithmic proficiency across diverse problem types, balancing efficiency, space constraints, and edge case handling for each distinct task.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Solve linked list, grid BFS, and median queries
Algorithmic Tasks
You are presented with three independent algorithmic tasks.
Task 1: Reorder a Linked List by Odd/Even Positions
Given the head of a singly linked list, reorder the nodes such that all nodes at odd indices come first, followed by all nodes at even indices (1-indexed positions). Within both the odd and even groups, the original relative order of nodes must be preserved.
- Input:
headof a singly linked list - Output: The head of the reordered list
- Constraints: O(1) extra space (excluding recursion stack space), O(n) time complexity.
Example: 1 -> 2 -> 3 -> 4 -> 5 becomes 1 -> 3 -> 5 -> 2 -> 4.
Task 2: Shortest Distance Between Any Two Islands (Grid Variant)
You are given an m x n grid composed of 0s and 1s, where 1 represents land and 0 represents water. An island is defined as a 4-directionally connected component of 1s.
You are allowed to convert water cells (0) to land cells (1). The "distance" between two islands is the minimum number of 0 cells that must be converted to create a 4-directional land path connecting them.
Your task is to return the minimum distance over all pairs of distinct islands.
- Input:
grid[m][n]of0/1 - Output: An integer representing the minimum number of flips
- Assumptions/Constraints:
m, ncan be up to approximately10^3. Design an efficient approach.- There are guaranteed to be at least two distinct islands in the grid.
Task 3: Find the Median Using a Rank-Count Oracle
There are N unknown integers, and you do not have direct access to the array containing them. You are provided with:
- The total count
Nof these integers. - An oracle function
countLessGreater(x)that, when called with an integerx, returns two integers:L(x): The count of numbers strictly less thanx.G(x): The count of numbers strictly greater thanx.
Using only calls to this oracle, find a median value.
- If
Nis odd, return the valuemsuch that exactly(N-1)/2numbers are less thanmand(N-1)/2numbers are greater thanm. - If
Nis even, return the lower median (defined as theN/2-th smallest value, using 1-indexed ranking).
State any additional assumptions you need (e.g., a known bounded integer range), and design an algorithm that minimizes the number of oracle queries.