← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Meta software engineering coding round, four questions back to back. Nothing too exotic but the pacing was real and a couple of them had wrinkles I didn't see coming until I was already mid-solution.

Questions Asked (4)

Q1

Given a string with lowercase letters and parentheses, remove the minimum number of characters to make the parentheses valid. Return any valid result.

Algorithms & Data Structures
Author's notes

Two-pass approach: left to right tracking unmatched closing parens, right to left for unmatched opens.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to track unmatched opening parentheses and a set to mark invalid closing parentheses. Then build the result by skipping marked characters, ensuring the minimum removals.

Pro tip: Clarify that 'minimum removals' means removing only characters that cause invalidity, and mention that multiple valid answers exist so any is acceptable.

1. Clarify the problem

Confirm that we need to remove the fewest characters to make parentheses valid, and that any valid result is acceptable.

2. Identify invalid parentheses

Traverse the string, using a stack to match opening and closing parentheses, and mark unmatched closing parentheses and leftover opening parentheses as invalid.

3. Build the result

Construct the output string by including only characters that are not marked for removal.

4. Analyze complexity

State that the time and space complexity are O(n), where n is the length of the string.

5. Test with examples

Walk through edge cases like empty string, all invalid parentheses, and nested valid parentheses to verify correctness.

Key Points to Mention

  • Stack-based approach for matching parentheses
  • Marking invalid characters to remove
  • Time and space complexity O(n)
  • Handling multiple valid outputs
  • Edge cases: empty string, no parentheses, all invalid
  • Alternative approaches like two-pass counting

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Find the lowest common ancestor of two nodes in a tree where each node has a parent pointer.

Algorithms & Data Structures
Author's notes

Classic two-pointer trick: walk both nodes up to root simultaneously, swapping to the other node's start when you hit null.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Since each node has a parent pointer, treat the problem as finding the intersection of two linked lists (the paths from each node to the root). Use a two-pointer technique: advance both pointers one step at a time, and when a pointer reaches the root, redirect it to the other node's starting position. They will meet at the LCA after at most O(h) steps, where h is the tree height.

Pro tip: Always clarify with the interviewer whether the nodes are guaranteed to be in the same tree and whether the parent pointers are reliable. Mentioning edge cases like one node being an ancestor of the other shows thoroughness and can lead to a more robust solution.

1. Clarify assumptions and edge cases

Confirm that both nodes exist in the same tree, parent pointers are valid, and discuss edge cases such as one node being the root or one node being an ancestor of the other.

2. Explain the two-pointer approach

Describe how to use two pointers starting at the given nodes, moving up via parent pointers, and redirecting to the other node's start when reaching the root, ensuring they meet at the LCA.

3. Analyze time and space complexity

State that the algorithm runs in O(h) time where h is the height of the tree, and uses O(1) extra space, which is optimal.

4. Discuss alternative approaches

Mention that using a hash set to store ancestors of one node and then checking the other node's ancestors also works in O(h) time but uses O(h) space, and compare trade-offs.

5. Handle edge cases and conclude

Walk through edge cases like one node being an ancestor of the other, or nodes at different depths, and confirm the solution handles them correctly.

Key Points to Mention

  • Parent pointers allow upward traversal, reducing the problem to finding the intersection of two linked lists.
  • Two-pointer technique with pointer redirection ensures O(h) time and O(1) space.
  • Time complexity is O(h) where h is the height of the tree, which is optimal.
  • Space complexity is O(1) with the two-pointer approach, versus O(h) with a hash set.
  • Edge cases: one node is an ancestor of the other, nodes at different depths, root node involvement.
  • The algorithm naturally handles nodes at different depths without needing to compute depths first.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Compute the depth-weighted sum of a nested integer list, where each integer is multiplied by its depth level (top-level is depth 1).

Algorithms & Data Structures
Author's notes

Recursive DFS, pass depth as a parameter.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the input structure and depth definition, then choose between recursive DFS and iterative BFS/stack. Implement the solution with careful handling of nested lists and integers, and analyze time and space complexity.

Pro tip: Meta interviewers value clean, bug-free code and clear communication. Start by walking through a simple example to confirm understanding, and discuss trade-offs between recursion and iteration (e.g., recursion depth limits).

1. Clarify the problem

Confirm that the input is a nested list where each element is either an integer or a list, and depth starts at 1 for top-level elements. Ask about constraints (e.g., maximum depth, size) and expected output type.

2. Choose an approach

Decide between recursive DFS (simpler, but may hit recursion limit) and iterative BFS/stack (explicit control, avoids recursion depth issues). Consider using a queue for BFS to process level by level.

3. Implement the solution

Write clean code: for DFS, pass depth as parameter and accumulate sum; for BFS, use a queue storing (element, depth) and process each element. Handle both integers and lists correctly.

4. Test with examples

Walk through a sample input like [1,[4,[6]]] to verify the sum (1*1 + 4*2 + 6*3 = 27). Also test edge cases: empty list, single integer, deeply nested list.

5. Analyze complexity

State time complexity O(N) where N is total number of elements (integers and lists), and space complexity O(D) for recursion stack or O(N) for queue in worst case.

Key Points to Mention

  • Depth definition: top-level is depth 1, each nested level increments depth by 1.
  • Recursive DFS: pass current depth, recurse on lists, add integer * depth.
  • Iterative BFS: use queue with (element, depth), process level by level.
  • Time complexity O(N) where N is total number of elements (including nested lists).
  • Space complexity: O(D) for recursion depth or O(N) for queue in worst case.
  • Edge cases: empty list, single integer, deeply nested list, large input.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

Return the K closest points to the origin from a list of 2D points.

Algorithms & Data Structures
Author's notes

Used a max-heap of size K, comparing squared distances to avoid the sqrt.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (e.g., input size, whether K is valid, and if the output order matters). Then discuss the trade-offs between sorting all points by distance (O(n log n)) and using a max-heap of size K (O(n log K)), and implement the optimal solution with clean code.

Pro tip: Avoid computing square roots by comparing squared distances to prevent floating-point precision issues and improve performance. Also, mention that for very large datasets, a quickselect-based approach can achieve average O(n) time.

1. Clarify requirements and constraints

Ask about input size, whether K is always valid, if the output needs to be sorted, and if duplicate points are allowed. This shows attention to detail and helps choose the right algorithm.

2. Discuss possible approaches

Explain the brute-force sort approach (O(n log n)) and the heap-based approach (O(n log K)). Mention that for large n and small K, the heap is more efficient.

3. Choose and justify the optimal approach

Select the max-heap of size K as the optimal solution, justifying it by time and space complexity. If K is close to n, sorting might be simpler and equally efficient.

4. Implement the solution

Write clean code using a max-heap (or priority queue) that stores points by their squared distance. Iterate through points, push to heap, and if size exceeds K, pop the farthest. Finally, extract the K points.

5. Analyze complexity and test edge cases

State time complexity O(n log K) and space O(K). Test with edge cases like K=0, K=n, duplicate points, and points with same distance.

Key Points to Mention

  • Use squared Euclidean distance to avoid floating-point precision issues and unnecessary square root computation.
  • Max-heap of size K efficiently maintains the K closest points, with time complexity O(n log K) and space O(K).
  • Sorting all points by distance is simpler but O(n log n) time, which may be suboptimal for large n and small K.
  • Quickselect can achieve average O(n) time but has worst-case O(n^2) and is more complex to implement.
  • Edge cases: K=0, K >= n, duplicate points, and points with identical distances.
  • If the output needs to be sorted, an additional O(K log K) sort is required; otherwise, the heap order is sufficient.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.