PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

Traverse levels selecting nodes meeting a predicate evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Traverse levels selecting nodes meeting a predicate

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given the root of a binary tree and a predicate P(value) that returns true for certain node values, return a list of lists where the i-th list contains the values at depth i that satisfy P. Implement an iterative traversal that uses a stack to manage nodes per level (no recursion). Clearly explain how you detect level boundaries, maintain per-level collections, and analyze time and space complexity.

Quick Answer: Traverse levels selecting nodes meeting a predicate evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given a binary tree and a predicate, return the values at each depth that satisfy the predicate, level by level. The tree is given as a **level-order list** `tree` (LeetCode style), where `null`/`None` marks a missing child. The predicate `P(value)` is concretized here as `value % k == 0` for a given positive integer `k`. Return a list of lists where the i-th list contains the values at depth `i` (left-to-right) that satisfy the predicate. **A depth with no satisfying values still contributes an empty list** for that level, so the number of returned lists equals the height of the tree. Implement an **iterative** traversal (no recursion). Use an explicit frontier/stack of the current level's nodes; detect level boundaries by processing one full level at a time and building the next level's frontier as you go. **Example** ``` tree = [1, 2, 3, 4, 5, 6, 7], k = 2 1 / \ 2 3 / \ / \ 4 5 6 7 Depth 0: [1] -> even: [] Depth 1: [2, 3] -> even: [2] Depth 2: [4,5,6,7] -> even: [4, 6] Result: [[], [2], [4, 6]] ``` If the tree is empty, return `[]`.

Constraints

  • 0 <= number of nodes <= 10^4
  • Node values fit in a 32-bit signed integer (may be negative or zero).
  • 1 <= k (the predicate divisor); P(v) is true iff v % k == 0.
  • tree is a level-order encoding; None/null marks a missing child.
  • A depth with zero satisfying values still yields an empty list, so len(result) equals the tree height.
  • Must be iterative — no recursion.

Examples

Input: ([1, 2, 3, 4, 5, 6, 7], 2)

Expected Output: [[], [2], [4, 6]]

Explanation: Full 3-level tree. Depth 0 has only 1 (odd) -> []. Depth 1 has 2,3 -> even: [2]. Depth 2 has 4,5,6,7 -> even: [4, 6].

Input: ([2, 4, 6, 8, 10, 12, 14], 2)

Expected Output: [[2], [4, 6], [8, 10, 12, 14]]

Explanation: Every value is even, so each level's full set of values is kept.

Hints

  1. Reconstruct the tree from the level-order list, or process the list directly — either way you need to know each node's depth.
  2. Process exactly one level at a time: hold the current level's nodes in a list (the frontier), and while scanning it, build the next level's frontier from the children.
  3. The level boundary is implicit: when you finish draining the current frontier, you have collected all satisfying values for that depth and assembled the complete next frontier.
  4. Append an empty list for a level even when no node there satisfies the predicate, so the i-th list always corresponds to depth i.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 9,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Choose the Cheapest Round Trip - Meta (medium)
  • Palindrome After Deleting at Most One Character - Meta (medium)
  • Validate Sorted Order Under a Custom Alphabet - Meta (medium)
  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)