KatInterview

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

Trees & Graphs Interview Problems

Practice 5976 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.

5976
Problems
2213
Auto-graded
15
Topics
271
Added today

Showing 151–180 of 349 results

  • 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 & 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 & 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 & 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 & 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
  • Trees & GraphsEasyAuto-graded
    Reporting Chain Length Between Two Employees

    Find the number of links on the path between two people in a company tree given as pairs, which tests undirected graph modeling and BFS or lowest common ancestor reasoning.

    Trees & Graphsgraphbfstreeshortest-path
  • Trees & GraphsMediumAuto-graded
    Words Traced Through a Letter Board

    Find every dictionary word that can be traced through orthogonally adjacent cells of a letter board without reusing a cell, using backtracking guided by a prefix tree.

    Trees & Graphsbacktrackingtriedfsgrid
  • Trees & GraphsMediumAuto-graded
    Nearest Exit Distance Map on a Walled Grid

    Compute, for every open cell of a grid with walls and exits, the shortest 4-directional distance to any exit using multi-source BFS, leaving unreachable cells unchanged.

    Trees & Graphsbfsgridmulti-source-bfsmatrix
  • Trees & GraphsMediumAuto-graded
    Service Inventory Filter and Dependency Risk Report

    Filter a service inventory by namespace, attributes and status, then compute transitive dependencies and risk flags from a root service, handling cycles and dangling references.

    Trees & Graphsgraph-traversaldfshash-mapfiltering
  • Trees & GraphsMediumAuto-graded
    Shortest Route to an Exit with Fixed Direction Priority

    Run BFS on a character grid to return the shortest move sequence to any exit, breaking ties by a fixed U, D, L, R direction priority.

    Trees & Graphsbfsgridshortest-pathpath-reconstruction
  • Trees & GraphsMediumAuto-graded
    Best Island Total with Row-Major Boundary Cells

    Find connected components in a signed grid and pick the best-scoring one, with and without a no-negatives rule, reporting its first and last row-major cells.

    Trees & Graphsgriddfsconnected-componentstie-breaking
  • Trees & GraphsMediumAuto-graded
    Costliest Root Task in a Task Forest

    Sum durations over each root's subtree in a parent-linked record list and return the root with the largest total, testing tree aggregation and iterative traversal.

    Trees & Graphstreedfshash-mapiterative-traversal
  • Trees & GraphsMediumAuto-graded
    Best Conversion Product Over Simple Currency Paths

    Find the largest product of edge weights over simple directed paths between two nodes, using backtracking DFS, with a special case for identical endpoints and an unreachable case.

    Trees & Graphsgraphdfsbacktrackingsimple-paths