← Meta Interview Insights

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

Intermediate
Jun 2026

Summary

Four questions across what felt like a pretty standard Meta coding round, mix of trees and arrays and one OOP design problem with a bunch of follow-ups that went longer than I expected. The difficulty ramp was real and by the fourth question I was running on fumes.

Questions Asked (4)

Q1

Given the root of a binary tree, print the node values level by level. Also state the time and space complexity of your solution.

Algorithms & Data Structures
Author's notes

Classic BFS question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that 'level by level' means a breadth-first traversal, then implement it using a queue. For each level, process all nodes currently in the queue, collect their values, and enqueue their children. After coding, state that the time complexity is O(n) and space complexity is O(w) where w is the maximum width of the tree.

Pro tip: At Meta, interviewers value clean, bug-free code and clear communication. Before coding, briefly discuss edge cases (empty tree, skewed tree) and mention that the queue-based BFS naturally handles level separation by tracking the queue size at each iteration.

1. Clarify the problem

Confirm that 'level by level' means printing nodes in breadth-first order, with each level on a new line or as separate lists. Ask if the output should be a list of lists or just printed values.

2. Choose the right data structure

Select a queue (e.g., collections.deque in Python) to perform BFS. Explain that a queue ensures nodes are processed in the order they are discovered, which naturally groups nodes by level.

3. Outline the algorithm

Describe the steps: initialize an empty queue and enqueue the root. While the queue is not empty, record the current queue size (number of nodes at this level), then iterate that many times: dequeue a node, add its value to the current level list, and enqueue its left and right children if they exist. After the inner loop, append the level list to the result.

4. Implement the solution

Write clean code with meaningful variable names. Handle the edge case of an empty tree by returning an empty list. Use a list to store the result if needed.

5. Analyze complexity

State that time complexity is O(n) because each node is visited once. Space complexity is O(w) where w is the maximum width of the tree, due to the queue holding at most one level's worth of nodes. In the worst case (perfect binary tree), w = n/2, so O(n).

Key Points to Mention

  • Breadth-first search (BFS) is the natural approach for level-order traversal.
  • Using a queue to track nodes and processing level by level via queue size.
  • Time complexity: O(n) where n is the number of nodes.
  • Space complexity: O(w) where w is the maximum width of the tree; worst-case O(n).
  • Edge cases: empty tree, skewed tree (which affects space complexity).
  • Potential follow-up: handling very large trees or using iterative vs recursive approaches.

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

Q2

Design a class that tracks strings and how many times each has been added, with methods to add a string, get its current count, and remove it. Follow-ups included computing the total count across all entries efficiently, generalizing to arbitrary data types and swappable data structures, and discussing concurrency and complexity trade-offs.

Algorithms & Data StructuresTechnical Trade-offsSystem Design
Author's notes

The base class was fine, I got through it pretty quickly with a hashmap.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements and designing a simple hash map-based solution with O(1) add, get, and remove operations. Then address follow-ups by introducing a running total for efficient total count, generalizing with generics, and discussing trade-offs of different data structures and concurrency approaches.

Pro tip: Proactively discuss the trade-offs between simplicity and scalability, and mention how you would test the class for correctness and performance under concurrent access.

1. Clarify Requirements

Ask questions to confirm expected operations, data types, concurrency needs, and performance goals. This ensures you design the right solution.

2. Design Core Solution

Propose a hash map (dictionary) to store strings and their counts, with methods for add, get, and remove. Analyze time and space complexity.

3. Address Follow-ups

For total count, suggest maintaining a running total variable. For arbitrary types, use generics. For swappable data structures, discuss alternatives like trees or tries and their trade-offs.

4. Discuss Concurrency

Explain how to make the class thread-safe using locks, concurrent data structures, or atomic operations, and the performance implications.

5. Summarize Trade-offs

Conclude by comparing approaches, highlighting when to choose each based on requirements like read/write ratio, memory, and concurrency.

Key Points to Mention

  • Hash map provides O(1) average time for add, get, and remove.
  • Maintain a running total for O(1) total count, updating on add and remove.
  • Use generics to support arbitrary data types, ensuring proper equals and hashCode.
  • Consider alternative data structures (e.g., balanced BST, trie) for different trade-offs.
  • Thread safety can be achieved with synchronized methods, ReentrantReadWriteLock, or ConcurrentHashMap.
  • Discuss memory overhead and potential collisions in hash map implementation.

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

Q3

In a binary tree of integers, find how many nodes have a value equal to the average of all values in their subtree (including themselves). Design an O(n) solution.

Algorithms & Data Structures
Author's notes

This one tripped me up at first because my instinct was a naive recursive approach that recomputes sums repeatedly.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order traversal to compute subtree sums and node counts bottom-up. At each node, check if its value equals the average of its subtree (sum / count) and count matches. This yields O(n) time and O(h) space.

Pro tip: Mention that you avoid floating-point division by comparing value * count == sum to prevent precision issues. Also, clarify that the average is computed as an integer division? Actually, the problem likely expects exact equality, so use multiplication.

1. Clarify the problem

Confirm that the average is the sum of all node values in the subtree divided by the number of nodes, and that we need exact equality (no rounding). Ask if the tree can be empty or have negative values.

2. Choose traversal

Select a post-order traversal (left, right, root) because we need subtree information before processing the current node.

3. Define recursive function

Write a function that returns (sum, count) for the subtree rooted at a node. For a null node, return (0,0). For a leaf, sum=node.val, count=1.

4. Check condition and count

After computing left and right subtree sums and counts, compute total sum and count. If node.val * total_count == total_sum, increment a global counter.

5. Analyze complexity

Explain that each node is visited once, so time is O(n). Space is O(h) for recursion stack, where h is tree height.

Key Points to Mention

  • Post-order traversal to compute subtree sums and counts bottom-up.
  • Avoid floating-point division by using multiplication: node.val * count == sum.
  • Use a global counter or pass by reference to accumulate matches.
  • Handle edge cases: empty tree, single node, negative values.
  • Time complexity O(n) because each node is processed once.
  • Space complexity O(h) due to recursion stack, which is O(n) worst-case for skewed tree.

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

Q4

Given an array of wood lengths and a number k, find the largest integer cut length L such that cutting all pieces produces at least k total pieces. Return 0 if it's not possible.

Algorithms & Data Structures
Author's notes

Binary search on the answer.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that the number of pieces obtained from a given cut length L is monotonic: as L increases, the total pieces decrease. Therefore, binary search on L between 1 and the maximum wood length, checking feasibility by summing floor(length / L) for all pieces. Return the largest feasible L, or 0 if even L=1 yields fewer than k pieces.

Pro tip: Clarify edge cases upfront: if k is 0, return the maximum length (or handle as specified); if the sum of all lengths is less than k, return 0 immediately. Also, use integer division and avoid floating-point to prevent precision issues.

1. Understand the problem and constraints

Restate the problem: given an array of wood lengths and integer k, find the largest integer L such that sum(floor(length_i / L)) >= k. If no such L exists, return 0. Confirm edge cases: k=0, empty array, k larger than total possible pieces.

2. Identify monotonicity and binary search range

Observe that as L increases, the total number of pieces decreases. So binary search on L from 1 to max(wood lengths). The answer is the largest L that satisfies the condition.

3. Implement feasibility check

For a given L, compute total pieces by summing length // L for each wood piece. If total >= k, L is feasible; otherwise, not. Use integer division to avoid floating-point errors.

4. Binary search for the optimal L

Initialize low=1, high=max_length, ans=0. While low <= high: mid = (low+high)//2. If feasible(mid), update ans=mid and low=mid+1; else high=mid-1. Return ans.

5. Analyze complexity and test edge cases

Time complexity: O(n log(max_length)), space O(1). Test with cases like k=0, k=1, k greater than total pieces, and all wood lengths equal.

Key Points to Mention

  • Monotonicity: the number of pieces is non-increasing as L increases, enabling binary search.
  • Binary search bounds: low=1, high=max(wood lengths), and handle the case where even L=1 gives fewer than k pieces (return 0).
  • Feasibility check: sum of floor(length / L) for all pieces, using integer division.
  • Time complexity: O(n log(max_length)), which is efficient for large inputs.
  • Edge cases: k=0, empty array, k larger than total possible pieces, and very large k.
  • Avoid floating-point arithmetic; use integer division to prevent precision issues.

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