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 1–30 of 434 results
Compute all possible configurations of n non-attacking queens on an n x n board using stateful depth-first search.
Model answerbacktrackingrecursiondepth-first-searchParse a JSON string into formatted stop lines and turn employee rows into a sorted, indented reporting-tree outline using an adjacency list and iterative DFS.
Auto-gradedjsontreedfssortingProduce the lexicographically smallest valid course order from prerequisite pairs, or an empty list if a cycle makes it impossible, using a topological sort with a min-heap.
Auto-gradedtopological-sortgraphheapcycle-detectionFind the minimum Manhattan distance between any pair of target characters in a 2D character grid.
Auto-gradedbfsmatrixdistance-transformgraphsConstruct a valid contiguous layout for multiple crop zones on a grid and find the shortest path avoiding edge-touching zones.
Model answertrees-graphsgridbfsconstructive-algorithmsFind every dictionary word that can be traced through adjacent, non-repeating cells of a letter grid, which rewards sharing prefixes across words instead of searching per word.
Auto-gradedtriebacktrackingdfsgridImplement topological sort, BFS, DFS and Dijkstra on a dependency graph, then discuss CSR versus adjacency lists, CPU/GPU parallel traversal, and deterministic testing.
Model answertopological-sortbfs-dfsdijkstracsrFind the fewest directed edges from a start node to a goal node in an acyclic graph, returning 0 for identical nodes and -1 when unreachable; tests BFS on unit-weight graphs.
Auto-gradedgraphbfsshortest-pathdagTraverse a binary tree given as nested lists level by level and keep only values from an allowed set, preserving left-to-right order and empty levels.
Auto-gradedbinary-treelevel-orderbfsstackTurn flat employee-manager records into a list of name groups by depth from the top manager, using a level-by-level breadth-first traversal.
Auto-gradedbfstreehash-maplevel-orderPerform a depth-bounded graph traversal over web pages based on domain filtering, and validate balanced brackets using a stack.
Auto-gradedbfsgraph-traversalstackstring-validationCombine square-and-multiply modular exponentiation with a multi-source BFS that labels every grid cell with its distance to the nearest source cell.
Auto-gradedbfsmodular-exponentiationgridmulti-source-bfsGroup records into clusters where records are linked when their name/email match score reaches a cutoff, using union-find and bucketing on exact field values.
Auto-gradedunion-findconnected-componentsgraphhashingCompute the anticlockwise perimeter of a binary tree given as child-index arrays, combining root, left edge, leaves and right edge without duplicates.
Auto-gradedbinary-treetree-traversaldfsiterativeRestrict a dependency graph to the ancestors of one goal node, then produce the smallest-ID-first topological order, or an empty list if a cycle blocks it.
Auto-gradedtopological-sortgraphheapcycle-detectionGiven a DAG of task dependencies and one start node, compute how many times each node fires when every activation fans out once along every outgoing edge.
Model answerdagtopological-sortpath-countinggraph-traversalCount 4-connected groups of land cells in a binary grid and report the area of the largest group, testing graph traversal with correct visited-state handling.
Auto-gradedgraphdfsbfsgridFind a shortest word chain where each step swaps one or two letters, with a fixed tie-break, using BFS on an implicit graph and wildcard bucketing for fast neighbor lookup.
Auto-gradedbfsshortest-pathstringshashingFind the fewest shuttle lines to board to get from one stop to another, using a graph search over lines and a stop-to-lines index to stay linear in total stops.
Auto-gradedbfsgraphhash-mapshortest-pathBuild a binary tree from a level-order list with gaps and report the first and last node of every level, testing BFS level separation and level-order parsing.
Auto-gradedbinary-treebfslevel-ordertree-constructionModel a sliding ball as BFS over resting positions, where each edge is a full roll until a wall or border, and the target counts only if the ball stops on it.
Auto-gradedbfsgridgraphshortest-pathReconstruct a binary tree given its inorder and postorder traversal arrays, assuming all node values are unique.
Model answertrees-graphsrecursionhash-tableDetermine if a given target sequence matches a top-down path in an N-ary tree represented by nested dictionaries.
Auto-gradedtrees-graphsrecursiondfstree-traversalCount connected components of 1-cells in a binary grid using only up/down/left/right adjacency, via flood fill or union-find.
Auto-gradedgraphbfsconnected-componentsmatrixProduce the lexicographically smallest valid ordering of a dependency graph using a min-heap variant of Kahn's algorithm, returning an empty list if a cycle makes ordering impossible.
Auto-gradedtopological-sortheapgraphcycle-detectionRecursively walk a JSON-like value alongside a simplified schema and report type and missing-key errors with exact paths, in deterministic order.
Auto-gradedrecursiondfsjsonvalidationCompare two keyed trees in parallel and count changed nodes, where a changed value replaces its whole subtree and unmatched nodes count with their subtrees.
Auto-gradedtreedfshash-mapsubtree-sizeParse a forgiving HTML string into an element tree in one pass, then collect each matching element's combined text in document order using an iterative depth-first traversal.
Auto-gradedparsingtreedfsstackDecode a level-order bit tree with skipped None slots, then sum the binary values read along each root-to-leaf path, testing BFS parsing and DFS with a running accumulator.
Auto-gradedbinary-treedfsbfslevel-order-parsingMatch couriers to parcels by repeatedly choosing the globally closest pair, using BFS shortest paths on a grid with walls and deterministic tie-breaking.
Auto-gradedbfsgridgreedysorting