Implement bounds, minimum, pathfinding, and moving average
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates proficiency with core data structures and algorithms—specifically array operations (merge and bounds), minimum-finding strategies, 2D graph pathfinding (BFS vs. DFS and path reconstruction), and streaming sliding-window statistics (moving average).
Merge Sorted Lists and Return Bounds
Constraints
- A and B are sorted
Examples
Input: ([1, 3, 3], [2, 3, 4], 3)
Expected Output: {'merged': [1, 2, 3, 3, 3, 4], 'lower': 2, 'upper': 5}
Explanation: Duplicates around x.
Input: ([], [1, 2], 0)
Expected Output: {'merged': [1, 2], 'lower': 0, 'upper': 0}
Explanation: Empty A.
Input: ([1, 2], [], 5)
Expected Output: {'merged': [1, 2], 'lower': 2, 'upper': 2}
Explanation: Upper past end.
Hints
- After merging, use binary search for first >= x and first > x.
Minimum Value in an Array
Examples
Input: ([3, 1, 2],)
Expected Output: 1
Explanation: Unsorted array.
Input: ([],)
Expected Output: None
Explanation: Empty input.
Input: ((-1, -5, 0),)
Expected Output: -5
Explanation: Tuple input with negatives.
Hints
- Scan every value unless sortedness can be guaranteed.
Path from Start to Any Grid Exit
Constraints
- 0=open, 1=wall
Examples
Input: ([[1, 1, 1], [1, 0, 0], [1, 1, 0]], (1, 1))
Expected Output: [(1, 1), (1, 2)]
Explanation: Path to right boundary.
Input: ([[0]], (0, 0))
Expected Output: [(0, 0)]
Explanation: Start already at exit.
Input: ([[1, 1], [1, 0]], (1, 1))
Expected Output: [(1, 1)]
Explanation: Boundary start at bottom-right.
Input: ([[1, 1, 1], [1, 0, 1], [1, 1, 1]], (1, 1))
Expected Output: []
Explanation: No exit.
Hints
- BFS gives a shortest path and parent pointers reconstruct it.
Moving Average of Last k Values
Constraints
- k > 0
Examples
Input: (3, [1, 10, 3, 5])
Expected Output: [1.0, 5.5, 4.666667, 6.0]
Explanation: Window grows then slides.
Input: (1, [4, 5])
Expected Output: [4.0, 5.0]
Explanation: Window size one.
Input: (5, [])
Expected Output: []
Explanation: Empty stream.
Hints
- Keep a queue of the last k values and a running sum.