← Meta Interview Insights

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

Intermediate
Apr 2026

Summary

Meta software engineer interview with three coding problems, two graph/tree questions and one string manipulation. Pretty standard algorithmic round, nothing too surprising.

Questions Asked (3)

Q1

Given a string with parentheses, remove the minimum number of characters to make it valid.

Algorithms & Data Structures
Author's notes

Stack-based approach, track indices of unmatched parens and remove them at the end.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem: we need to remove the minimum number of parentheses to make the string valid, meaning every opening parenthesis has a matching closing parenthesis in the correct order. Use a stack to track unmatched opening parentheses and a set to mark unmatched closing parentheses, then build the result by skipping marked indices. Alternatively, use two passes to count and remove invalid parentheses.

Pro tip: After presenting the stack solution, mention that a two-pass counting approach can achieve O(1) space if the output can be built in place or if we only need the length. This shows awareness of space optimization, which is valued at Meta.

1. Clarify the problem and edge cases

Confirm that we need to remove the minimum number of parentheses to make the string valid, and that the relative order of remaining characters is preserved. Discuss edge cases: empty string, already valid string, string with only invalid parentheses, and multiple valid solutions.

2. Choose an approach

Decide between a stack-based solution (O(n) time, O(n) space) and a two-pass counting solution (O(n) time, O(1) space). Explain the trade-offs and pick one to implement, typically the stack approach for clarity.

3. Implement the solution

For the stack approach: iterate through the string, push indices of '(' onto a stack, and for ')' either pop if stack is non-empty or mark the index as invalid. After the first pass, mark all indices left in the stack as invalid. Then build the result by skipping marked indices.

4. Test with examples

Walk through examples like '(()', ')()', '()())', and 'a)b(c' to verify correctness. Ensure the output is valid and has minimum removals.

5. Analyze complexity and discuss optimizations

State time and space complexity: O(n) time and O(n) space for the stack approach. Mention that a two-pass counting method can reduce space to O(1) if we only need the length or can modify the string in place.

Key Points to Mention

  • Use a stack to track unmatched opening parentheses and a set to mark invalid closing parentheses.
  • Two-pass approach: first pass left-to-right to remove invalid ')', second pass right-to-left to remove invalid '('.
  • Time complexity O(n) and space complexity O(n) for stack, or O(1) for two-pass counting.
  • Preserve the relative order of remaining characters.
  • Handle edge cases: empty string, already valid, all invalid.
  • Minimum removals means we only remove characters that cannot be part of any valid sequence.

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

Q2

In a binary grid, you can flip one 0 to a 1. Find the size of the largest island you can form after the flip.

Algorithms & Data Structures
Author's notes

This one took me a minute to get the right approach.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, identify all existing islands and label each cell with a unique island ID, recording the size of each island. Then, for each water cell (0), compute the sum of sizes of distinct neighboring islands plus one, and track the maximum. If no water cells exist, return the size of the largest island.

Pro tip: Clarify edge cases upfront, such as when the grid is all 1s or all 0s, and discuss the trade-offs between DFS/BFS and Union-Find, showing you consider both correctness and efficiency.

1. Clarify and Restate

Confirm the problem constraints: grid dimensions, whether flipping is mandatory, and what to return if no 0 exists. Restate the goal to ensure alignment.

2. Identify Existing Islands

Use DFS, BFS, or Union-Find to traverse the grid, assign a unique ID to each island, and record its size in a map or array.

3. Evaluate Each Water Cell

For every 0, look at its four neighbors, collect the distinct island IDs, sum their sizes, add 1 for the flipped cell, and update the maximum.

4. Handle Edge Cases

If there are no 0s, return the size of the largest existing island. If the grid is all 0s, flipping one cell yields an island of size 1.

5. Analyze Complexity

State that the time and space complexity are O(R*C) for an R x C grid, as each cell is visited a constant number of times.

Key Points to Mention

  • Use DFS/BFS or Union-Find to label islands and compute their sizes efficiently.
  • For each 0, consider only distinct neighboring islands to avoid double-counting.
  • Handle edge cases: all 1s, all 0s, and grids with no water cells.
  • Time and space complexity: O(R*C) time and O(R*C) space for visited/ID storage.
  • Optimization: avoid revisiting cells by marking visited or using a visited set.
  • Discuss trade-offs: DFS recursion depth vs. BFS queue memory vs. Union-Find overhead.

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

Q3

Return the values visible from the right side of a binary tree, level by level.

Algorithms & Data Structures
Author's notes

BFS, grab the last node at each level.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the problem asks for the rightmost node at each level, then choose between BFS (level-order traversal) and DFS (preorder traversal with depth tracking). For BFS, process each level and record the last node; for DFS, track the maximum depth seen so far and update the result when visiting a node at a new depth, prioritizing the right child first.

Pro tip: At Meta, interviewers value clean, efficient code and clear communication. Start by discussing the trade-offs between BFS and DFS (e.g., BFS uses O(width) space, DFS uses O(height) space) and pick the one that best fits the constraints, then write modular code with meaningful variable names.

1. Clarify the problem

Confirm that the output should be a list of values from the rightmost node at each level, ordered from top to bottom. Ask about edge cases like an empty tree or a tree with only left children.

2. Choose an approach

Decide between BFS and DFS based on space complexity and simplicity. BFS is straightforward: use a queue and process level by level. DFS is more memory-efficient for skewed trees: use recursion with depth tracking.

3. Implement the solution

For BFS: initialize a queue with the root, while the queue is not empty, iterate over the current level size, and add the last node's value to the result. For DFS: recursively traverse right child first, and if the current depth equals the result size, append the node's value.

4. Test with examples

Walk through a sample tree (e.g., [1,2,3,null,5,null,4]) and verify the output matches expectations. Also test edge cases: empty tree, single node, and a left-skewed tree.

5. Analyze complexity

State the time complexity: O(n) for both approaches since each node is visited once. Space complexity: O(w) for BFS where w is the maximum width, and O(h) for DFS where h is the height (due to recursion stack).

Key Points to Mention

  • Level-order traversal (BFS) using a queue to process nodes level by level.
  • Depth-first search (DFS) with preorder traversal, prioritizing the right child, and tracking depth.
  • Time complexity O(n) and space complexity O(w) for BFS or O(h) for DFS.
  • Handling edge cases: empty tree returns empty list, single node returns [root.val].
  • The importance of visiting the right child first in DFS to ensure the first node encountered at each depth is the rightmost.
  • Using a result list where its size indicates the number of levels processed so far.

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