Meta interview prep
12 practice questions on the topics candidates reported from Meta interviews, with answers. The questions are ours; each one links to the interview report that names its topic.
DSA 11
-
You merge n sorted arrays of k elements each into one sorted array, using a min-heap that holds the smallest unused element of each array. What is the time complexity?
- O(nk log n)
- O(nk log nk)
- O(nk)
- O(n^2 k)
Show the answer
A. O(nk log n)
There are nk elements, and each is pushed and popped once on a heap of at most n entries, O(log n) each. Joining the arrays and sorting everything would cost O(nk log nk).
This topic is named in a candidate’s interview report: New grad onsite, 2022, on LeetCode.
-
The sorted array [4, 5, 6, 7, 0, 1, 2] has been rotated. A modified binary search looks for 0. Why can binary search still work, and how fast is it?
- At every step at least one half of the range is sorted, which tells you which half can hold the target; O(log n)
- You must scan for the rotation point, so it is O(n) at best
- The array must be sorted again first, so it takes O(n log n)
- It only works when the array has not been rotated
Show the answer
A. At every step at least one half of the range is sorted, which tells you which half can hold the target; O(log n)
Compare the middle with the ends: one side is always in order. If the target lies within that sorted side's range, search there, otherwise search the other side. Each step halves the range.
This topic is named in a candidate’s interview report: SDE on-campus interview, 2019, on Code360.
-
Kadane's algorithm finds the maximum sum of a contiguous subarray. For [-2, 1, -3, 4, -1, 2, 1, -5, 4], what is that sum and which indices does it cover?
- 6, indices 3 to 6
- 7, indices 3 to 8
- 5, indices 0 to 6
- 4, index 3 only
Show the answer
A. 6, indices 3 to 6
Keep the best sum ending at each index: extend the previous run if it is positive, otherwise start again. The best run is 4, -1, 2, 1 at indices 3 to 6, summing to 6, in one O(n) pass.
This topic is named in a candidate’s interview report: Software engineer interview, 2018, on Code360.
-
A disjoint set union uses union by rank and path compression. What is the cost per operation?
- O(n) per operation
- O(log n) exactly
- Amortised O(alpha(n)), the inverse Ackermann function, which is at most 4 for any realistic n
- O(1) in the worst case for every single call
Show the answer
C. Amortised O(alpha(n)), the inverse Ackermann function, which is at most 4 for any realistic n
Path compression points nodes straight at their root as find walks up; union by rank keeps trees shallow. Together any sequence of m operations costs O(m alpha(n)), effectively linear.
This topic is named in a candidate’s interview report: SDE on-campus interview, 2021, on Code360.
-
How do you find the first and last positions of a target in a sorted array with duplicates in O(log n)?
- Binary search once, then walk left and right from the match
- Two binary searches: one that keeps going left after a match (lower bound) and one that keeps going right (upper bound)
- Scan the array from both ends
- Hash every value to its positions
Show the answer
B. Two binary searches: one that keeps going left after a match (lower bound) and one that keeps going right (upper bound)
Walking outwards from one match is O(n) when the target fills the array. Lower bound finds the first index with a value >= target and upper bound the first > target; the last position is one before that.
This topic is named in a candidate’s interview report: New grad onsite, 2022, on LeetCode.
-
Buildings stand in a row with heights [4, 2, 3, 1], and the ocean is to the right. A building has a view if every building to its right is shorter. Which indices have a view?
- [0, 2, 3]
- [3]
- [0, 1, 2, 3]
- [0, 3]
Show the answer
A. [0, 2, 3]
Scan from the right keeping the tallest height seen so far: 1 has a view, 3 beats 1, 2 does not beat 3, and 4 beats 3. Reverse the collected indices to get [0, 2, 3], in O(n).
This topic is named in a candidate’s interview report: New grad onsite, 2022, on LeetCode.
-
What is the length of the longest strictly increasing subsequence of [10, 9, 2, 5, 3, 7, 101, 18]?
- 3
- 5
- 4
- 2
Show the answer
C. 4
For example 2, 3, 7, 101 (or 2, 5, 7, 18). The O(n^2) DP tries every earlier element; the O(n log n) method keeps, for each length, the smallest possible tail and binary searches where each number goes.
This topic is named in a candidate’s interview report: SDE on-campus interview, 2019, on Code360.
-
You remove the 2nd node from the end of 1 -> 2 -> 3 -> 4 -> 5 in one pass with two pointers. What is the list after?
- 1 -> 2 -> 3 -> 5
- 1 -> 2 -> 4 -> 5
- 1 -> 3 -> 4 -> 5
- 1 -> 2 -> 3 -> 4
Show the answer
A. 1 -> 2 -> 3 -> 5
Move a fast pointer n steps ahead, then move both until fast reaches the last node: slow then sits just before the node to remove, 3 here, and slow.next = slow.next.next drops 4. A dummy node before the head handles removing the head itself.
This topic is named in a candidate’s interview report: Business technology analyst interview, 2022, on Code360.
-
Merging the overlapping intervals among [1, 3], [2, 6], [8, 10] and [15, 18] leaves how many intervals?
- 4
- 3
- 2
- 1
Show the answer
B. 3
Sorted by start, [1, 3] and [2, 6] overlap and become [1, 6]; [8, 10] and [15, 18] stand alone. After sorting, one pass that extends the last merged interval or starts a new one: O(n log n).
This topic is named in a candidate’s interview report: SDE on-campus interview, 2019, on Code360.
-
Which pair of traversals pins down one binary tree when all keys are distinct?
- Preorder and postorder
- Inorder together with preorder (or with postorder)
- Preorder alone
- Level order alone
Show the answer
B. Inorder together with preorder (or with postorder)
Preorder's first key is the root, and finding it in the inorder sequence splits the rest into the left and right subtrees, recursively. Preorder with postorder cannot tell a lone left child from a lone right child.
This topic is named in a candidate’s interview report: SDE on-campus interview, 2021, on Code360.
-
In the grid below, 2 is a rotten orange, 1 a fresh one and 0 empty. Each minute, fresh oranges next to a rotten one (up, down, left, right) rot. How many minutes until none are fresh? Rows: [2, 1, 1], [1, 1, 0], [0, 1, 1].
- 3
- 4
- 5
- Never
Show the answer
B. 4
A multi-source BFS starts from every rotten orange at once and counts levels; the bottom-right orange is reached last, after 4 minutes. If any fresh orange is never reached, the answer is -1.
This topic is named in a candidate’s interview report: Business technology analyst interview, 2022, on Code360.
Python 1
-
What does this print?
def ok(s, skip=True): i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: if not skip: return False return ok(s[i + 1:j + 1], False) or ok(s[i:j], False) i, j = i + 1, j - 1 return True print(ok("abca"), ok("abc"))- True True
- False False
- False True
- True False
Show the answer
D. True False
"abca" fails at b and c; deleting the c leaves "aba", a palindrome. "abc" fails at a and c, and neither "bc" nor "ab" is a palindrome. At most one deletion keeps it O(n).
This topic is named in a candidate’s interview report: New grad onsite, 2022, on LeetCode.