KatInterview

practice a little every day — you've got this ♡

Trees & Graphs Interview Problems

Practice 5978 original coding, SQL, system design, machine learning and behavioral problems across 15 topics. Coding and SQL problems run against hidden test cases right in your browser; every problem has hints and a worked solution, free.

5978
Problems
2214
Auto-graded
15
Topics
272
Added today

Showing 181–210 of 434 results

  • Trees & GraphsMediumAuto-graded
    Can Every Task Be Scheduled Given Dependency Pairs?

    Tests modeling dependencies as a directed graph and detecting whether a cycle prevents a complete valid ordering, e.g. via indegree-based topological sorting.

    Trees & Graphsgraphtopological-sortcycle-detectionbfs
  • Trees & GraphsHardAuto-graded
    Calculate Subtree Totals in a Tree

    Given an undirected tree and node weights, compute the sum of values for every subtree rooted at node 1.

    Trees & Graphstrees-graphsdfstree-traversalbottom-up
  • Trees & GraphsEasyAuto-graded
    Extract Sorted Values from Level-Order Encoded BST

    Extract all valid integer elements from a level-order serialized binary search tree and return them in non-decreasing order.

    Trees & Graphstrees-graphsheaps-sortingbinary-search-tree
  • Trees & GraphsMediumAuto-graded
    Best Net Reward Route to an Underscore Node in a DAG

    Find the highest net score of a route in a weighted DAG that ends at a node whose name starts with an underscore, using topological order and DP.

    Trees & Graphsgraphdagtopological-sortdynamic-programming
  • Trees & GraphsMediumAuto-graded
    Common Ancestor of Two Values, Only If Both Exist

    Find the lowest common ancestor of two values in a level-order encoded binary tree, returning null when either value is missing from the tree.

    Trees & Graphsbinary-treelowest-common-ancestorparent-mapbfs
  • Trees & GraphsMediumAuto-graded
    Maximum Subtree Weight for Every Node in a Network

    Find the maximum sum of a connected component containing each node in an undirected tree using dynamic programming with rerooting.

    Trees & Graphstrees-graphsdynamic-programmingtree-dprerooting-dp
  • Trees & GraphsMediumModel answer
    Evaluate Grid Path Feasibility and Identify Top K Elements

    Determine matrix path reachability under obstacle constraints and extract the k most frequent elements from an array.

    Trees & Graphstrees-graphsheaps-sortingarrays
  • Trees & GraphsMediumAuto-graded
    Referral Credit Ledger with Multilevel Propagation

    Simulate account additions, referral bonus propagation (direct or indirect), and threshold queries ordered deterministically.

    Trees & Graphstrees-graphsarraysdesign-ds
  • Trees & GraphsMediumAuto-graded
    BFS Unweighted Shortest Path with Ancestor Tracking

    Perform breadth-first traversal on an unweighted graph to compute shortest distances and record parent pointers for path reconstruction.

    Trees & Graphsbfsgraph-traversalshortest-pathqueue
  • Trees & GraphsMediumAuto-graded
    Lightest Downward Path in a Level-Order Tree

    Find the root-to-leaf path with the smallest value sum in a tree given as a level-order list, breaking ties toward the left, without recursion on 100,000-node skewed trees.

    Trees & Graphsbinary-treedfsiterative-traversalprefix-sum
  • Trees & GraphsMediumAuto-graded
    Read a Tree Off Column by Column from a Level-Order List

    Decode a level-order tree list, assign each node a column, and output values grouped by column with row-then-reading-order tie-breaking.

    Trees & Graphsbinary-treebfshash-maplevel-order
  • Trees & GraphsMediumAuto-graded
    Depth-Scaled Total of a Nested Integer Structure

    Traverse an arbitrarily nested list of integers and add up each value multiplied by how deeply it is nested, testing DFS/BFS traversal of recursive structures.

    Trees & Graphsdfsbfsnested-listsrecursion
  • Trees & GraphsMediumAuto-graded
    Cheapest Edit Count to Turn a Tree into a Perfect Tree

    Find the minimum number of leaf deletions and node insertions that make a binary tree perfect, using per-level counts and a bounded search over the target height.

    Trees & Graphsbinary-treebfslevel-ordermath
  • Trees & GraphsMediumAuto-graded
    Recover the Preorder of a BST from Its Postorder Listing

    Turn the postorder listing of a distinct-valued BST into its preorder listing in linear time, including chain-shaped trees, using a monotonic stack over the reversed input.

    Trees & Graphsbinary-search-treemonotonic-stacktree-traversalreconstruction
  • Trees & GraphsMediumModel answer
    Traverse a Concurrent Dependency Graph with Rate Control and Retries

    Discover the full transitive dependency graph of a package using sequential BFS and concurrent worker pools with retry handling and exact termination logic.

    Trees & Graphstrees-graphsdesign-dsstacks-queuesconcurrency
  • Trees & GraphsMediumAuto-graded
    Reachable Vaults and Weighted Bracket Nesting Score

    Combine graph reachability from a start node with computing a nesting-weighted score of a balanced bracket string, returning both results as a pair.

    Trees & Graphsgraph-traversaldfsstringsparentheses
  • Trees & GraphsMediumAuto-graded
    Column-by-Column Readout of a Level-Order Encoded Tree

    Compute the column-wise readout of a binary tree given as a level-order array, ordering nodes by column, then row, then value.

    Trees & Graphsbinary-treebfshash-mapsorting
  • Trees & GraphsMediumAuto-graded
    Audit a Workflow Run Against Dependencies and Precedence Rules

    Check a run order against a task dependency graph, mandatory tasks and extra precedence pairs, while rejecting duplicates, unknown tasks and cyclic constraints.

    Trees & Graphsgraphtopological-sortcycle-detectionhash-map
  • Trees & GraphsMediumModel answer
    Collect Every Reachable Page on a Single Host

    Write a function that explores a website from one starting URL, following only links on the same hostname, and returns each distinct page once while coping with link cycles.

    Trees & Graphsgraph-traversalbfsurl-normalizationdeduplication
  • Trees & GraphsMediumModel answer
    Resolve Out-of-Order Variable Assignments in a Mini Language

    Parse lines of integer assignments using only + and -, resolve forward and chained references, and report undefined names and cycles in a well-defined way.

    Trees & Graphstopological-sortparsingdependency-graphcycle-detection
  • Trees & GraphsMediumAuto-graded
    Longest Dependency Chain in a Task DAG

    Find the longest total-duration chain in a task dependency DAG and return its length and task names, using topological-order DP with predecessor tracking.

    Trees & Graphsgraphdagtopological-sortdynamic-programming
  • Trees & GraphsMediumAuto-graded
    Acyclic Invitation Graph with Depth Queries and Expected Payout

    Simulate a stream of operations on a DAG: reject cycle-forming or duplicate edges, count nodes within a hop limit, and sum expected decayed payouts using BFS shortest distances.

    Trees & Graphsgraphbfsdagcycle-detection
  • Trees & GraphsEasyModel answer
    Lone-Parity Filter and Leftmost Node Per Tree Depth

    Write one function that returns the elements whose even/odd class appears exactly once in an array, plus the first node's value at every depth of a binary tree.

    Trees & Graphsbfsbinary-treelevel-ordercounting
  • Trees & GraphsMediumAuto-graded
    Job Scheduling with Cycle Check and Downstream Rerun Set

    Topologically sort jobs with cycle detection, then use graph reachability to find all jobs affected by changed ones and list them in valid order.

    Trees & Graphstopological-sortgraphbfscycle-detection
  • Trees & GraphsHardAuto-graded
    Fewest Rounds to Slay a Scripted Boss With One Revive

    Find the minimum number of rounds to defeat a boss that attacks on a repeating script, using state-space BFS over boss HP, player HP, revive flag, and script position.

    Trees & Graphsbfsstate-space-searchsimulationshortest-path
  • Trees & GraphsMediumAuto-graded
    List Every Non-Repeating Route in a Directed Graph

    Enumerate all routes from a source to a target in a directed graph that never revisit a node, returned in lexicographic order, using backtracking DFS.

    Trees & Graphsgraphdfsbacktrackingrecursion
  • Trees & GraphsMediumAuto-graded
    Left-Packed Tree Check from a Level-Order Array

    Decide whether a binary tree given as a level-order array with None placeholders is complete, by rebuilding it and scanning breadth-first for a node that follows a gap.

    Trees & Graphsbinary-treebfslevel-ordertree-construction
  • Trees & GraphsMediumAuto-graded
    Open One Wall to Speed Up Multi-Source Spread

    Use multi-source BFS and brute force over single wall removals to minimize the time until every target is reached, with strict-improvement tie-breaking.

    Trees & Graphsbfsgridmulti-source-bfsbrute-force
  • Trees & GraphsMediumAuto-graded
    Surviving Tree Height and Extra Removals to Meet a Limit

    Compute the height of a tree after removing marked nodes (cutting off their subtrees), then find the fewest extra removals needed to bring the height within a limit.

    Trees & Graphstreedfsiterative-traversalheight
  • Trees & GraphsMediumAuto-graded
    Two-Choice Clinic Booking: Can Everyone Get a Slot?

    Decide whether each patient can get one of their two acceptable slots with no slot reused, by modelling slots as graph vertices and patients as edges.

    Trees & Graphsunion-findgraph-modelingmatchingconnected-components