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 121–150 of 434 results
Remove a node and its entire subtree from a forest stored as a parent array, either by marking slots with -1 or by compacting indices and remapping parent pointers.
Auto-gradedforestdfsparent-arrayreindexingBuild a mine-count grid with mines in row-major order, then use DFS backtracking to check whether a word can be traced through a separate letter grid.
Auto-gradedgridbacktrackingdfssimulationCount groups of orthogonally touching '1' cells in a character grid, a connected-components task solved with an iterative flood fill or union-find.
Auto-gradedgraphbfsgridconnected-componentsCompute the shortest path distance between two nodes in an unweighted undirected graph using Breadth-First Search.
Auto-gradedbfsgraphshortest-pathbreadth-first-searchDetermine character precedence in an unfamiliar alphabet using a sorted list of terms, returning the lexicographically first valid character sequence or an empty string for invalid inputs.
Auto-gradedtrees-graphsheaps-sortingstringsCount the minimum non-root nodes to remove from a parent-array tree, splicing children upward, so the height is at most a limit; tests subtree heights and a path-decomposition argument.
Auto-gradedtreesubtree-heightiterative-bfsgreedyRecursively copy an n-ary tree while dropping every node whose label contains a given substring, together with its subtree, without mutating the input.
Auto-gradedtreerecursiondfsstringsFind every player comparable to all others in a consistent win/loss DAG, using transitive reachability in both directions, ideally with bitsets.
Auto-gradedgraphtopological-sorttransitive-closurebitsetProduce the lexicographically smallest topological ordering of a directed graph, or report that no ordering exists because of a cycle.
Auto-gradedtopological-sortheapgraphcycle-detectionCompute the height of an index-encoded binary tree without deep recursion, and the minimum rooms needed for half-open time intervals, returned together as a pair.
Auto-gradedbinary-treeiterative-dfsintervalssweep-lineAnswer k-th ancestor queries on a rooted tree efficiently using binary lifting.
Auto-gradedtrees-graphsmath-bitsarraysCount connected components in an undirected graph given as an n x n adjacency matrix, where links chain together transitively.
Auto-gradedgraphconnected-componentsdfsunion-findFind the k-th smallest key in a binary search tree represented as a level-order array.
Auto-gradedtrees-graphstreesin-order-traversalbinary-search-treeDecide whether a string can be traced through a rectangular letter grid by stepping to up/down/left/right neighbors without reusing a cell, using backtracking search.
Auto-gradedbacktrackingdfsmatrixpruningFind the deepest node whose subtree contains two given nodes in a tree stored as child lists, using a single depth-first pass that stays safe on very deep trees.
Auto-gradedtreedfslowest-common-ancestorpost-orderRun a per-mode BFS on a grid to find step counts, then choose the mode with least total time, then least total cost, then smallest index.
Auto-gradedbfsgridshortest-pathtie-breakingParse feature dependency specs, run modular checksum verification, and evaluate user eligibility and feature graph dependencies while handling cyclic rollouts.
Model answertrees-graphsstringsdepth-first-searchSolve a task dependency ordering problem via topological sort and build an expression evaluator supporting custom operator precedence, unary signs, and overflow checks.
Model answertopological-sortgraph-algorithmsexpression-parserstacksFind a valid topological sequence for tasks with prerequisite constraints or detect cyclic dependencies.
Auto-gradedgraphtopological-sortbfsdfsCompute the integer value of every variable defined as a sum of literals and other variables, raising distinct errors for duplicate, undefined, or circular definitions.
Model answertopological-sortgraphkahn-algorithmcycle-detectionCompute a tree's longest path in edges and the shortest 4-directional route through a grid with obstacles, returning both results together.
Auto-gradedbfstree-diametershortest-pathgridCount connected components in an undirected graph given as a symmetric adjacency matrix, without modifying the input, using graph traversal or union-find.
Auto-gradedgraphconnected-componentsdfsunion-findTwo keys in a binary search tree were exchanged by mistake. Restore ordering by swapping values back, keeping the shape intact, in linear time and constant extra space.
Model answerbinary-search-treemorris-traversalin-ordertree-recoveryEvaluates breadth-first graph search and set tracking to traverse a network of web pages and find an exit target.
Auto-gradedtrees-graphsbreadth-first-searchhash-mapProduce the deterministic bottom-up deletion sequence for a file tree given as flat path lists, using an iterative post-order traversal that survives very deep nesting.
Auto-gradedpost-orderdfsiterativestackConvert an unordered flat list of records with parent links into a nested forest, preserving input order and rejecting duplicate ids, missing parents and cycles.
Auto-gradedtree-constructionhash-mapcycle-detectionforestClassify every node of a rooted tree given as [id, parent_id] pairs into Root, Inner, or Leaf, using a set of parent ids, and return the labels sorted by id.
Auto-gradedtreehash-setclassificationsortingCalculate the minimum non-negative value additions needed so that the node sum of every subtree in a binary tree equals K, returning -1 if impossible.
Model answertrees-graphspost-ordergreedydepth-first-searchSimulate one tap on a minesweeper grid, using a flood fill from zero-count cells to reveal connected safe regions and their numbered borders.
Auto-gradedbfsflood-fillgridsimulationCrawl every page reachable from a seed URL on the same host, ignoring URL fragments, first with one thread and then with a thread-safe parallel design.
Model answerbfsgraph-traversalconcurrencythread-pool