← Meta Interview Insights

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

Senior
May 2026

Summary

Meta coding round, two algorithm problems back to back. Both were medium-to-hard level and required solid reasoning about correctness, not just getting to a solution.

Questions Asked (2)

Q1

Given a string of only '(' and ')' characters, what is the minimum number of parentheses you need to insert (anywhere) to make it a valid, balanced string? Describe a linear-time algorithm and justify why it works.

Algorithms & Data Structures
Author's notes

This one I actually felt okay about.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a single left-to-right scan with a counter to track unmatched closing parentheses and the balance of open parentheses. The minimum insertions equals the number of unmatched closing parentheses plus the final balance of unmatched open parentheses. Justify that each unmatched closing parenthesis requires an insertion before it, and each unmatched open requires an insertion after it, and these insertions are sufficient.

Pro tip: Mention that the algorithm runs in O(n) time and O(1) space, and that it can be easily adapted to return the actual inserted string if needed. Also, note that the problem is equivalent to finding the minimum additions to make a parentheses string valid, which is a common interview warm-up at Meta.

1. Clarify the problem and constraints

Confirm that the string contains only '(' and ')', and that insertions can be made anywhere. State that the goal is to minimize the total number of insertions.

2. Design the linear-time algorithm

Initialize two counters: open_needed = 0 and close_needed = 0. Iterate through each character: if '(', increment open_needed; if ')', check if open_needed > 0, then decrement open_needed, else increment close_needed. After the loop, the answer is open_needed + close_needed.

3. Explain the counters

open_needed tracks the number of unmatched '(' that need a matching ')' later. close_needed tracks the number of unmatched ')' that need a matching '(' before them. Each unmatched ')' forces an insertion of '(' before it; each unmatched '(' forces an insertion of ')' after it.

4. Justify correctness

Argue that any valid string must have at least close_needed '(' inserted before the unmatched ')' and at least open_needed ')' inserted after the unmatched '('. These insertions are sufficient because inserting them yields a balanced string. Thus the sum is minimal.

5. Analyze complexity and edge cases

State that the algorithm runs in O(n) time and O(1) space. Mention edge cases: empty string (0 insertions), all '(' (insert n ')'), all ')' (insert n '('), and already balanced (0 insertions).

Key Points to Mention

  • Single-pass left-to-right scan with two counters: one for unmatched open parentheses and one for unmatched close parentheses.
  • Minimum insertions = unmatched close parentheses + unmatched open parentheses.
  • Each unmatched close parenthesis requires an insertion of '(' before it; each unmatched open requires an insertion of ')' after it.
  • The algorithm is optimal because any valid string must satisfy these lower bounds, and the insertions achieve them.
  • Time complexity O(n), space complexity O(1).
  • Edge cases: empty string, all open, all close, already balanced.

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

Q2

Given strings s and t, find the shortest contiguous substring of s that contains every character of t with at least the same frequency. Return empty string if none exists. Walk through the algorithm, time and space complexity, and key edge cases.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Classic sliding window but the frequency matching part tripped me up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window with two pointers to expand and contract the window while maintaining character counts, tracking the minimum length that satisfies the frequency requirement. Explain the algorithm step-by-step, analyze time and space complexity, and discuss edge cases such as empty strings or missing characters.

Pro tip: Mention that the window never needs to shrink below the required length, and that you can optimize by skipping characters not in t. This shows you understand the problem deeply and can optimize beyond the basic solution.

1. Clarify and Define

Restate the problem: find the shortest contiguous substring of s that contains all characters of t with at least the same frequency. Confirm edge cases like empty s or t, and characters not in t.

2. Choose Data Structures

Use a frequency map for t and a dynamic frequency map for the current window. Maintain a counter for how many characters of t are satisfied (i.e., have reached the required frequency).

3. Sliding Window Algorithm

Expand the right pointer to include characters, updating counts and the satisfied counter. When all characters are satisfied, shrink from the left while maintaining satisfaction, updating the minimum window length and start index.

4. Complexity Analysis

Time: O(|s| + |t|) because each character is visited at most twice (once by right, once by left). Space: O(|s| + |t|) for the frequency maps, but can be reduced to O(|t|) if using a fixed-size array for ASCII.

5. Edge Cases and Testing

Discuss cases: t longer than s, no valid window, duplicate characters in t, and characters in s not in t. Walk through a small example to verify correctness.

Key Points to Mention

  • Sliding window technique with two pointers (left and right).
  • Frequency maps for t and the current window, and a 'satisfied' counter to track how many characters meet the required frequency.
  • Time complexity O(|s| + |t|) and space complexity O(|s| + |t|) or O(|t|) with optimization.
  • Edge cases: empty strings, t longer than s, no valid substring, and characters in s not in t.
  • Optimization: skip characters not in t to reduce unnecessary window expansions.
  • Correctness: the window is only shrunk when all characters are satisfied, ensuring minimal length.

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