← Meta Interview Insights

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

Senior
Jun 2026

Summary

Meta coding round, two algorithm problems back to back. Both were more involved than I expected for a single session, especially the second one which I hadn't really thought about before.

Questions Asked (2)

Q1

You have an array of characters where 'w' means workday and 'h' means holiday, plus an integer n representing PTO days you can use to flip workdays into days off. Find the maximum length contiguous vacation you can get. Walk through your function signature, an O(n) solution, complexity analysis, and edge cases like all workdays, all holidays, n exceeding the workday count, or empty input.

Algorithms & Data Structures
Author's notes

Sliding window, which I got to pretty quickly.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window to find the longest contiguous subarray with at most n 'w's. Expand the right pointer, count workdays, and shrink from the left when the count exceeds n, tracking the maximum window length. Discuss the function signature, complexity, and edge cases to show thoroughness.

Pro tip: Clarify that 'n' represents the maximum number of workdays you can flip, so the window can contain at most n 'w's. Mention that the same approach works if 'n' is larger than the total workdays (return whole array) or if the array is empty (return 0).

1. Define the function signature

Specify input parameters (char array and integer n) and return type (integer max vacation length). For example, in Java: int maxVacation(char[] days, int n).

2. Explain the sliding window approach

Maintain a window [left, right] and a count of workdays within it. Expand right, increment workday count if days[right] == 'w'. When count > n, shrink from left until count <= n. Update max length at each step.

3. Walk through a small example

Use a concrete example like ['w','h','w','w','h'] and n=1 to illustrate how the window expands and contracts, and how the maximum length is found.

4. Analyze time and space complexity

Time complexity is O(m) where m is the array length, since each element is visited at most twice. Space complexity is O(1) as only a few variables are used.

5. Cover edge cases

Discuss: all workdays (answer = n if n < m, else m), all holidays (answer = m), n >= total workdays (answer = m), empty input (answer = 0), and n = 0 (longest contiguous holidays).

Key Points to Mention

  • Sliding window technique for O(n) time
  • Maintaining a count of workdays in the current window
  • Shrinking the window when workday count exceeds n
  • Updating the maximum length after each expansion
  • Handling edge cases: empty array, all workdays, all holidays, n >= workday count
  • Time and space complexity analysis

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

Q2

Given a sorted non-decreasing array where the number of distinct values is tiny relative to the total length, count the unique values faster than O(n). Describe an approach that exploits the sorted structure, analyze its complexity, and explain when it actually beats a linear scan.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one tripped me up more than I'd like to admit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use binary search to find the next distinct value, starting from the first element. Repeatedly binary search for the first index where the value is greater than the current value, and count each distinct value found. This runs in O(k log n) time, where k is the number of distinct values, which is faster than O(n) when k log n < n.

Pro tip: Clarify that the problem guarantees the array is sorted and k is tiny; if the array were unsorted, hashing would be O(n) and optimal. Also, mention that if k is close to n, a linear scan is better, so the approach is adaptive.

1. Understand the problem and constraints

Restate that the array is sorted non-decreasing and the number of distinct values k is very small compared to n. The goal is to count distinct values faster than O(n).

2. Propose binary search approach

Explain that you can use binary search to jump to the next distinct value. Start at index 0, count it, then binary search for the first index where the value is greater than the current value. Repeat until the end of the array.

3. Analyze complexity

Each binary search takes O(log n) time, and we perform k binary searches, so total time is O(k log n). Space is O(1). Compare to O(n) linear scan: this is faster when k log n < n, i.e., when k is small relative to n/log n.

4. Discuss trade-offs and edge cases

Mention that if k is large (close to n), linear scan is better. Also handle edge cases: empty array, all elements same, etc. Note that the approach relies on sorted order; if unsorted, hashing would be O(n) and optimal.

5. Conclude with when it beats linear scan

Summarize that the binary search approach beats linear scan when the number of distinct values is small enough that k log n < n. For example, if k is O(1), it's O(log n), much faster than O(n).

Key Points to Mention

  • Binary search to find the next distinct value (first index where value > current).
  • Time complexity O(k log n) where k is number of distinct values.
  • Space complexity O(1).
  • Comparison with linear scan: faster when k log n < n, i.e., k < n/log n.
  • Edge cases: empty array, all elements same, k close to n.
  • Alternative: if array unsorted, hashing gives O(n) but not better; sorted structure enables faster.

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