Find Neighboring Records by Identifier
Company: Ziprecruiter
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of ordered data traversal and efficient lookup design, assessing competency with data structures, indexing and reasoning about time and space complexity for repeated neighbor queries.
Constraints
- 0 <= len(records) <= 200000
- 0 <= len(operations) <= 200000
- Each record contains a unique string key `"id"`
- Each operation is either `("next", id)` or `("prev", id)`
Examples
Input: ([{"id": "u1", "name": "Ann"}, {"id": "u2", "age": 30}, {"id": "u3", "active": True}], [("next", "u1"), ("prev", "u3"), ("next", "u3"), ("prev", "u1"), ("next", "missing")])
Expected Output: ["u2", "u2", None, None, None]
Explanation: The ordered ids are ["u1", "u2", "u3"]. So the next id after "u1" is "u2", the previous id before "u3" is "u2", and the boundary or missing-id lookups return None.
Input: ([], [("next", "x"), ("prev", "x")])
Expected Output: [None, None]
Explanation: There are no records, so every lookup returns None.
Input: ([{"id": "only", "value": 10}], [("next", "only"), ("prev", "only"), ("prev", "missing")])
Expected Output: [None, None, None]
Explanation: A single record has neither a previous nor a next neighbor, and a missing id also returns None.
Input: ([{"id": "alpha"}, {"id": "beta", "score": 7}, {"id": "gamma"}, {"id": "delta", "flag": False}], [("prev", "gamma"), ("next", "beta"), ("prev", "alpha"), ("next", "delta")])
Expected Output: ["beta", "gamma", None, None]
Explanation: The list order is preserved as ["alpha", "beta", "gamma", "delta"]. So gamma's previous is beta, beta's next is gamma, alpha has no previous, and delta has no next.
Input: ([{"id": "id-10", "x": 1}, {"id": "id-2", "x": 2}, {"id": "id-30", "x": 3}], [("next", "id-10"), ("next", "id-2"), ("prev", "id-30")])
Expected Output: ["id-2", "id-30", "id-2"]
Explanation: Neighboring records depend on input order, not lexicographic order of the ids.
Hints
- If you scan the list for every lookup, repeated queries become too slow. Preprocess the records once.
- Store each record's position by `id` in a hash map. Then the neighboring record is just at index `i - 1` or `i + 1` if that index is in bounds.