← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026Remote

Summary

Meta software engineering interview with a pretty involved coding question on top-k frequent elements. More theoretical than I expected, they kept pushing on complexity analysis and edge cases well past the initial solution.

Questions Asked (4)

Q1

Given an integer array and an integer k, return the k most frequent values. When frequencies tie, break ties first by smaller numeric value, then by earlier first occurrence in the array. Handle the case where k exceeds the number of distinct values.

Algorithms & Data Structures
Author's notes

I jumped straight to sorting by frequency which they immediately flagged as O(n log n).

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the tie-breaking rules and edge cases, then propose a solution using a hash map to count frequencies and first occurrences, followed by sorting the distinct values with a custom comparator. Discuss time and space complexity, and handle k exceeding distinct values by returning all distinct values sorted appropriately.

Pro tip: Mention that you can avoid sorting all distinct values by using a heap of size k with a custom comparator, but note that sorting is simpler and often sufficient; also, explicitly state that you will return all distinct values if k exceeds the number of distinct values.

1. Clarify requirements and edge cases

Confirm tie-breaking rules (smaller value first, then earlier first occurrence) and how to handle k > number of distinct values. Ask if the array can be empty or contain negative numbers.

2. Design the algorithm

Use a hash map to count frequencies and record the first occurrence index of each distinct value. Then sort the distinct values using a custom comparator that orders by frequency descending, then value ascending, then first occurrence ascending.

3. Implement and test

Write clean code, handling the case where k exceeds distinct count by returning all distinct values. Test with examples including ties and k larger than distinct count.

4. Analyze complexity

State time complexity: O(n + d log d) where n is array length and d is number of distinct values. Space complexity: O(d). Mention that a heap-based approach can achieve O(n + d log k) but sorting is simpler.

Key Points to Mention

  • Hash map for frequency counting and first occurrence tracking
  • Custom comparator for sorting: frequency descending, value ascending, first occurrence ascending
  • Edge case: k exceeds number of distinct values
  • Time and space complexity analysis
  • Alternative heap-based approach for better efficiency when k is small
  • Handling negative numbers and empty array

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

Q2

Compare the trade-offs between bucket counting by frequency, a size-k min-heap, and a quickselect-based partition for solving top-k frequency problems. What are the time and space complexities of each?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where things got uncomfortable.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by framing the problem: given an array of elements, find the k most frequent. Then compare the three approaches by their time and space complexities, highlighting when each is optimal. Conclude with a recommendation based on constraints like k, n, and memory.

Pro tip: Mention that bucket sort achieves O(n) time by leveraging the fact that frequencies are bounded by n, but it requires O(n) extra space. For streaming or memory-constrained scenarios, a size-k min-heap is preferable despite O(n log k) time.

1. Clarify the problem and assumptions

Restate the top-k frequent elements problem: given an array of size n, return the k most frequent elements. Assume k ≤ n and frequencies are integers.

2. Describe bucket counting by frequency

Explain: compute frequencies with a hash map, then create buckets indexed by frequency (1 to n). Collect elements from highest frequency buckets until k are found. Time: O(n), Space: O(n).

3. Describe size-k min-heap approach

Explain: compute frequencies with a hash map, then maintain a min-heap of size k. For each element, if heap size < k, push; else if frequency > heap min, replace. Time: O(n log k), Space: O(n) for map + O(k) for heap.

4. Describe quickselect-based partition

Explain: compute frequencies, then use quickselect to partition the unique elements by frequency to find the k-th largest. Average time: O(n), worst-case O(n^2). Space: O(n) for map + O(1) extra if in-place.

5. Compare trade-offs and recommend

Compare: bucket sort is fastest (O(n)) but uses O(n) space; heap is O(n log k) and good for streaming or when k is small; quickselect is average O(n) but has worst-case O(n^2) and is not stable. Recommend based on constraints.

Key Points to Mention

  • Time complexity: bucket O(n), heap O(n log k), quickselect O(n) average / O(n^2) worst.
  • Space complexity: bucket O(n), heap O(n + k), quickselect O(n) for frequency map.
  • Bucket sort leverages frequency bounds (1 to n) to achieve linear time.
  • Heap is ideal for streaming data or when k is much smaller than n.
  • Quickselect provides average linear time but has worst-case quadratic time and is not stable.
  • Trade-offs include memory usage, worst-case performance, and suitability for dynamic data.

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

Q3

For an array of length n, what is the maximum number of distinct frequency values possible across its unique elements? Derive and justify the formula.

Algorithms & Data Structures
Author's notes

Completely blanked.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that frequency values are positive integers summing to n. Then, to maximize the number of distinct frequencies, assign the smallest possible distinct positive integers (1, 2, 3, ...) to as many elements as possible, and derive the maximum k such that the sum of the first k integers is ≤ n. Finally, justify that this is optimal because any set of k distinct positive integers has sum at least k(k+1)/2.

Pro tip: Mention that the remaining elements can be absorbed by increasing the largest frequency without creating a new distinct value, ensuring the bound is tight. This shows you understand both the upper bound and the construction.

1. Clarify the problem

State that we are considering an array of length n, and we want to maximize the number of distinct frequency values among its unique elements. Frequencies are positive integers that sum to n.

2. Establish the upper bound

If there are k distinct frequencies, they must be at least the k smallest positive integers: 1, 2, ..., k. Their sum is k(k+1)/2, which cannot exceed n. So k(k+1)/2 ≤ n.

3. Solve for k

Solve the quadratic inequality k^2 + k - 2n ≤ 0 to get k ≤ floor((sqrt(8n+1)-1)/2). This gives the maximum possible number of distinct frequencies.

4. Show achievability

Construct an array with frequencies 1, 2, ..., k, and add the remaining n - k(k+1)/2 elements to the largest frequency (or distribute them without creating new distinct values). This achieves exactly k distinct frequencies.

5. Conclude and state the formula

Therefore, the maximum number of distinct frequency values is floor((sqrt(8n+1)-1)/2). Mention that this is the largest integer k such that k(k+1)/2 ≤ n.

Key Points to Mention

  • Frequencies are positive integers that sum to n.
  • To maximize distinct frequencies, use the smallest possible distinct positive integers.
  • The sum of the first k positive integers is k(k+1)/2.
  • The inequality k(k+1)/2 ≤ n must hold.
  • The maximum k is floor((sqrt(8n+1)-1)/2).
  • Achievability: assign frequencies 1 through k, and add any remaining elements to the largest frequency without creating a new distinct value.

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

Q4

How would you validate and guard against invalid inputs such as non-integer elements, negative or non-integer k, or extremely large arrays, while keeping the service robust?

System DesignTechnical Trade-offs
Author's notes

Felt like a throwaway follow-up but they actually engaged with it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the function's contract and expected input domain, then layer defenses: validate types and ranges early, enforce resource limits, and fail gracefully with clear errors. Emphasize that robustness comes from a combination of input validation, defensive coding, and operational safeguards like timeouts and monitoring.

Pro tip: Show you think beyond the happy path by discussing how you'd test edge cases and monitor for invalid inputs in production—this demonstrates ownership and a quality-first mindset that Meta values.

1. Clarify requirements and contract

Ask clarifying questions about the function's purpose, expected input types, and acceptable ranges. Define what 'valid' means and what behavior is expected for invalid inputs (e.g., throw exception, return error code).

2. Implement input validation

Check types (e.g., integer, array), ranges (e.g., k >= 0), and sizes (e.g., array length within limits). Use language-specific features like type guards or validation libraries, and fail fast with descriptive errors.

3. Add resource guards

For extremely large arrays, enforce size limits or use streaming/chunking to avoid memory exhaustion. Consider timeouts and circuit breakers to prevent resource exhaustion from malicious or accidental inputs.

4. Design for graceful degradation

Ensure the service remains responsive under invalid inputs by isolating failures (e.g., try-catch, error boundaries) and returning meaningful error responses without crashing.

5. Test and monitor

Write unit tests for edge cases (non-integer, negative k, huge arrays) and add logging/metrics to detect invalid input patterns in production, enabling proactive fixes.

Key Points to Mention

  • Type checking and range validation (e.g., Number.isInteger, k >= 0)
  • Array size limits and memory considerations (e.g., max length, streaming)
  • Error handling strategies: exceptions vs. error returns, and clear error messages
  • Defensive programming: fail fast, avoid silent failures
  • Testing edge cases: unit tests, property-based testing, fuzzing
  • Operational safeguards: timeouts, rate limiting, monitoring/alerting

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