Solve linked-list and top-K algorithm tasks
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates competency in fundamental data structures and algorithmic techniques, including linked list manipulation, frequency counting and top‑K retrieval, closest-point geometric queries, and stack-based string processing for repeated deletions.
Kth Node From the End of a Linked List
Constraints
- 1 <= number of nodes <= 10^4
- Node values fit in a 32-bit signed integer
- 1 <= k, but k may exceed the list length (return null in that case)
Examples
Input: ([1, 2, 3, 4, 5], 2)
Expected Output: 4
Explanation: 2nd node from the end of [1,2,3,4,5] is 4.
Input: ([1], 1)
Expected Output: 1
Explanation: Single node; the last node is the node itself.
Hints
- Think of two pointers: move a fast pointer k steps ahead first.
- Then advance fast and slow together until fast falls off the end; slow is now at the k-th node from the end.
- Equivalently, the k-th node from the end is at 0-based index n - k from the front.
Top K Frequent Elements
Constraints
- 1 <= nums.length <= 10^5
- Values fit in a 32-bit signed integer
- 1 <= k <= number of distinct values in nums
- Tie-break: equal frequencies ordered by value ascending
Examples
Input: ([1, 1, 1, 2, 2, 3], 2)
Expected Output: [1, 2]
Explanation: 1 appears 3x, 2 appears 2x — the two most frequent.
Input: ([1], 1)
Expected Output: [1]
Explanation: Only one distinct value.
Hints
- Count occurrences with a hash map first.
- You don't need to fully sort: a min-heap of size k, or bucket sort indexed by frequency, gives the top k efficiently.
- For deterministic output, break frequency ties by the smaller value.
K Closest Points to the Origin
Constraints
- 1 <= points.length <= 10^4
- Coordinates fit in a 32-bit signed integer
- 1 <= k <= points.length
- Use squared distance to compare; tie-break by x then y ascending
Examples
Input: ([[1, 3], [-2, 2]], 1)
Expected Output: [[-2, 2]]
Explanation: dist^2 of [-2,2] is 8, less than [1,3]'s 10.
Input: ([[3, 3], [5, -1], [-2, 4]], 2)
Expected Output: [[3, 3], [-2, 4]]
Explanation: dist^2: [3,3]=18, [-2,4]=20, [5,-1]=26 — closest two.
Hints
- Compare by squared distance x*x + y*y — no need for sqrt.
- A max-heap of size k keeps the k smallest distances seen so far.
- Quickselect partitions around the k-th smallest distance in O(n) average time.
Collapse Adjacent Identical Characters Until Stable
Constraints
- 0 <= s.length <= 10^5
- s consists of lowercase (and/or uppercase) letters
- A run of length >= 2 is removed entirely, not pairwise
- Removal cascades: collapsing may create new runs that must also be removed
Examples
Input: ('abbba',)
Expected Output: ''
Explanation: 'bbb' removed -> 'aa' -> 'aa' removed -> ''.
Input: ('abba',)
Expected Output: ''
Explanation: 'bb' removed -> 'aa' -> removed -> ''.
Hints
- Maintain a stack of (character, count) pairs as you scan left to right.
- When the incoming character matches the stack top, increment its count; when a count reaches the run threshold, remove that entry.
- After removing an entry, the new top and the next incoming character may match — that is the cascade, and the stack handles it automatically.