Implement solutions to several coding tasks
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
You are given several independent coding interview tasks. Solve each with the required time/space complexity.
## Problem 1: Find the minimum in a rotated sorted array
You are given an array `nums` of **distinct** integers that was originally sorted in increasing order and then rotated at an unknown pivot (e.g., `[4,5,6,7,0,1,2]`).
- **Input:** `nums: int[]`
- **Output:** the minimum value in `nums`
- **Constraints:** `1 ≤ len(nums) ≤ 2e5`
- **Expected complexity:** `O(log n)` time, `O(1)` extra space
## Problem 2: Time-based key-value store (variation)
Design a data structure that supports storing and querying values for a key over time.
Support the following operations:
- `set(key: string, value: string, timestamp: int) -> void`
- Stores the value for `key` at the given timestamp.
- `get(key: string, timestamp: int) -> string`
- Returns the value associated with `key` at the **largest timestamp ≤ query timestamp**.
- If there is no such timestamp for the key, return an empty string.
Assumptions/constraints:
- Timestamps are integers.
- For the same key, `set()` calls may arrive in increasing timestamp order (state your assumption); handle the general case if not.
- Target: near `O(log m)` per `get()` for `m` versions of a key.
## Problem 3: Lowest common ancestor in a binary tree
Given the root of a binary tree and two nodes `p` and `q` that both exist in the tree, return their **lowest common ancestor (LCA)**.
- The LCA of `p` and `q` is the lowest node in the tree that has both `p` and `q` as descendants (a node can be a descendant of itself).
## Problem 4: Mark a path on a maze
You are given:
- A 2D maze grid `maze` represented as a list of equal-length strings.
- `'#'` = wall
- `'.'` = open cell
- `'e'` = entrance (must stay as `'e'`)
- `'E'` = exit (must stay as `'E'`)
- A list `path` of coordinates (row, col) representing a valid path through the maze **including the entrance and exit**.
Write a function that returns a new maze where every coordinate in `path` is replaced with `'*'`, **except** the entrance `'e'` and exit `'E'`, which must not be changed.
- **Input:** `maze: string[]`, `path: (int row, int col)[]`
- **Output:** `string[]` (the updated maze)
- You may assume `path` contains only in-bounds coordinates and forms a valid path through open cells.
Quick Answer: This multipart prompt evaluates algorithmic problem-solving and data-structure design skills across several areas: finding a minimum in a rotated sorted array (binary search), implementing a time-based key-value store, computing lowest common ancestor in a binary tree, and marking a path on a 2D maze.
Minimum in Rotated Sorted Array
Return the minimum value in a distinct rotated sorted array.
Examples
Input: ([4, 5, 6, 7, 0, 1, 2],)
Expected Output: 0
Explanation: Rotated.
Input: ([1, 2, 3],)
Expected Output: 1
Explanation: Not rotated.
Input: ([2],)
Expected Output: 2
Explanation: Single value.
Time-Based Key-Value Store
Process set/get operations and return the latest value whose timestamp is <= query time.
Examples
Input: ((('set', 'room', 'A', 10), ('set', 'room', 'B', 20), ('get', 'room', 5), ('get', 'room', 15), ('get', 'room', 25)),)
Expected Output: ['', 'A', 'B']
Explanation: Prompt behavior.
Input: ((('get', 'x', 1),),)
Expected Output: ['']
Explanation: Missing key.
Lowest Common Ancestor in a Binary Tree
Given a unique-valued binary tree in level-order form, return the LCA value of p and q.
Constraints
- None represents a missing node; values are unique
Examples
Input: ([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 1)
Expected Output: 3
Explanation: Root LCA.
Input: ([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 4)
Expected Output: 5
Explanation: Ancestor case.
Input: ([1], 1, 1)
Expected Output: 1
Explanation: Same node.
Mark a Path on a Maze
Return a new maze with path cells marked by *, preserving entrance e and exit E.
Examples
Input: (['e..', '##.', '..E'], [(0, 0), (0, 1), (0, 2), (1, 2), (2, 2)])
Expected Output: ['e**', '##*', '..E']
Explanation: Preserves e and E.
Input: (['eE'], [(0, 0), (0, 1)])
Expected Output: ['eE']
Explanation: Only endpoints.