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.
Input: ([7, 8, 9], 3)
Expected Output: 7
Explanation: 3rd from the end equals the 1st from the front.
Input: ([10, 20, 30, 40], 1)
Expected Output: 40
Explanation: 1st from the end is the tail.
Input: ([5, 6], 3)
Expected Output: None
Explanation: k=3 exceeds the length 2, so it is out of range.
Input: ([], 1)
Expected Output: None
Explanation: Empty list has no nodes.
Input: ([-1, -2, -3], 2)
Expected Output: -2
Explanation: Negative values are handled; 2nd from end is -2.
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.
Input: ([4, 4, 5, 5, 6], 2)
Expected Output: [4, 5]
Explanation: 4 and 5 each appear 2x; tie broken by smaller value first.
Input: ([7, 7, 7], 1)
Expected Output: [7]
Explanation: Single distinct value repeated.
Input: ([3, 1, 2], 3)
Expected Output: [1, 2, 3]
Explanation: All frequencies equal (1); ordered by value ascending.
Input: ([-1, -1, -2, -2, -2, 0], 2)
Expected Output: [-2, -1]
Explanation: -2 appears 3x, -1 appears 2x.
Input: ([5, 5, 5, 5], 1)
Expected Output: [5]
Explanation: All occurrences are the same 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.
Input: ([[0, 0]], 1)
Expected Output: [[0, 0]]
Explanation: Point at the origin has distance 0.
Input: ([[1, 1], [1, -1], [-1, 1]], 3)
Expected Output: [[-1, 1], [1, -1], [1, 1]]
Explanation: All dist^2 = 2; tie broken by x then y ascending.
Input: ([[2, 2], [2, 2]], 1)
Expected Output: [[2, 2]]
Explanation: Duplicate points; one closest point returned.
Input: ([[10, 0], [1, 1], [0, 5]], 2)
Expected Output: [[1, 1], [0, 5]]
Explanation: dist^2: [1,1]=2, [0,5]=25, [10,0]=100 — 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 -> ''.
Input: ('abc',)
Expected Output: 'abc'
Explanation: No adjacent duplicates, nothing collapses.
Input: ('aabccba',)
Expected Output: 'a'
Explanation: 'aa' and 'cc' go -> 'bba' -> 'bb' goes -> 'a'.
Input: ('',)
Expected Output: ''
Explanation: Empty input stays empty.
Input: ('aaa',)
Expected Output: ''
Explanation: A single run of 3 disappears entirely.
Input: ('a',)
Expected Output: 'a'
Explanation: A lone character has no run to collapse.
Input: ('abcddcba',)
Expected Output: ''
Explanation: 'dd' -> 'abccba' -> 'cc' -> 'abba' -> 'bb' -> 'aa' -> ''.
Input: ('aabbaa',)
Expected Output: ''
Explanation: Each pair collapses and cascades down to empty.
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.