Topic
Trees & Graphs interview problems
Traversals, shortest paths, topological order and union-find. 434 original problems, each with hidden test cases you can run in the browser.
25 easy362 medium47 hard
Showing 181–210 of 434 results
Tests modeling dependencies as a directed graph and detecting whether a cycle prevents a complete valid ordering, e.g. via indegree-based topological sorting.
Auto-gradedgraphtopological-sortcycle-detectionbfsGiven an undirected tree and node weights, compute the sum of values for every subtree rooted at node 1.
Auto-gradedtrees-graphsdfstree-traversalbottom-upExtract all valid integer elements from a level-order serialized binary search tree and return them in non-decreasing order.
Auto-gradedtrees-graphsheaps-sortingbinary-search-treeFind 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.
Auto-gradedgraphdagtopological-sortdynamic-programmingFind the lowest common ancestor of two values in a level-order encoded binary tree, returning null when either value is missing from the tree.
Auto-gradedbinary-treelowest-common-ancestorparent-mapbfsFind the maximum sum of a connected component containing each node in an undirected tree using dynamic programming with rerooting.
Auto-gradedtrees-graphsdynamic-programmingtree-dprerooting-dpDetermine matrix path reachability under obstacle constraints and extract the k most frequent elements from an array.
Model answertrees-graphsheaps-sortingarraysSimulate account additions, referral bonus propagation (direct or indirect), and threshold queries ordered deterministically.
Auto-gradedtrees-graphsarraysdesign-dsPerform breadth-first traversal on an unweighted graph to compute shortest distances and record parent pointers for path reconstruction.
Auto-gradedbfsgraph-traversalshortest-pathqueueFind 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.
Auto-gradedbinary-treedfsiterative-traversalprefix-sumDecode a level-order tree list, assign each node a column, and output values grouped by column with row-then-reading-order tie-breaking.
Auto-gradedbinary-treebfshash-maplevel-orderTraverse 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.
Auto-gradeddfsbfsnested-listsrecursionFind 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.
Auto-gradedbinary-treebfslevel-ordermathTurn 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.
Auto-gradedbinary-search-treemonotonic-stacktree-traversalreconstructionDiscover the full transitive dependency graph of a package using sequential BFS and concurrent worker pools with retry handling and exact termination logic.
Model answertrees-graphsdesign-dsstacks-queuesconcurrencyCombine graph reachability from a start node with computing a nesting-weighted score of a balanced bracket string, returning both results as a pair.
Auto-gradedgraph-traversaldfsstringsparenthesesCompute the column-wise readout of a binary tree given as a level-order array, ordering nodes by column, then row, then value.
Auto-gradedbinary-treebfshash-mapsortingCheck a run order against a task dependency graph, mandatory tasks and extra precedence pairs, while rejecting duplicates, unknown tasks and cyclic constraints.
Auto-gradedgraphtopological-sortcycle-detectionhash-mapWrite 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.
Model answergraph-traversalbfsurl-normalizationdeduplicationParse lines of integer assignments using only + and -, resolve forward and chained references, and report undefined names and cycles in a well-defined way.
Model answertopological-sortparsingdependency-graphcycle-detectionFind the longest total-duration chain in a task dependency DAG and return its length and task names, using topological-order DP with predecessor tracking.
Auto-gradedgraphdagtopological-sortdynamic-programmingSimulate 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.
Auto-gradedgraphbfsdagcycle-detectionWrite 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.
Model answerbfsbinary-treelevel-ordercountingTopologically sort jobs with cycle detection, then use graph reachability to find all jobs affected by changed ones and list them in valid order.
Auto-gradedtopological-sortgraphbfscycle-detectionFind 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.
Auto-gradedbfsstate-space-searchsimulationshortest-pathEnumerate all routes from a source to a target in a directed graph that never revisit a node, returned in lexicographic order, using backtracking DFS.
Auto-gradedgraphdfsbacktrackingrecursionDecide 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.
Auto-gradedbinary-treebfslevel-ordertree-constructionUse multi-source BFS and brute force over single wall removals to minimize the time until every target is reached, with strict-improvement tie-breaking.
Auto-gradedbfsgridmulti-source-bfsbrute-forceCompute 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.
Auto-gradedtreedfsiterative-traversalheightDecide 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.
Auto-gradedunion-findgraph-modelingmatchingconnected-components