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 31–60 of 434 results
Decode a level-order binary tree with null gaps, then return the k-th value in its inorder sequence, or -1 if fewer than k nodes exist.
Auto-gradedbinary-treeinorder-traversaliterative-traversalstackAnswer permission queries over a DAG of groups where each group inherits from its parents, using graph traversal with memoization or caching.
Auto-gradedgraphdagdfsmemoizationChoose a hub node and up to k adjacent nodes to maximize the total node value, which tests per-node greedy selection over adjacency lists.
Auto-gradedgraphgreedysortingadjacency-listDecide whether a goal node can be reached from node 0 in a directed graph without ever stepping on a forbidden node, using BFS or DFS reachability.
Auto-gradedgraphbfsreachabilitydirected-graphReplay unsorted timed link events and find the earliest timestamp at which every node is in one connected group, using union-find over events sorted by time.
Auto-gradedunion-findgraph-connectivitysortingkruskalBuild a forest from (employee, manager) pairs and output each person with their depth in a sorted depth-first order, handling very deep chains iteratively.
Auto-gradedtreedfsforesthash-mapImplement a single function that extracts leaves and a shape template from nested lists and dicts, then rebuilds the original from them, testing recursion and traversal order.
Auto-gradedrecursiondfsnested-structuresdictionariesFind the binary tree node value that appears across the highest number of unique depth levels.
Model answertrees-graphsbreadth-first-searchhash-tableCompute, for every cell of a grid with walls and exits, the fewest steps to the nearest exit, marking walls and cut-off cells as -1, using multi-source BFS.
Auto-gradedbfsgridmulti-source-bfsshortest-pathCompress a square character grid into a four-way region tree, then locate a batch of words in the grid by walking neighbors in eight directions without reusing cells.
Model answerquadtreetriebacktrackingdivide-and-conquerFind the minimum total cost between two nodes of a directed graph with non-negative edge weights, returning -1 if no route exists. Tests shortest-path reasoning with a priority queue.
Auto-gradeddijkstrashortest-pathgraphheapSort an integer list without built-in sorting, then run a script of insert, search, delete and in-order commands on a duplicate-free binary search tree built from scratch.
Auto-gradedbinary-search-treequicksortrecursiontree-deletionDecide whether the vertices of an undirected multigraph can be split into two groups so every edge crosses between them, handling self-loops, repeated edges, and isolated nodes.
Auto-gradedgraphbfsbipartiteconnected-componentsCompute shortest-path distances from one source in an undirected graph with non-negative weights, handling parallel edges, self-loops, and unreachable nodes.
Auto-gradeddijkstrashortest-pathgraphheapFor each node in a DAG whose links may name unknown items, count the distinct nodes reachable along link direction, counting shared descendants once.
Auto-gradedgraphdagtopological-sortbitsetDecide whether a directed graph given as an adjacency list contains a cycle, handling self-loops, parallel edges, disconnected parts and an empty graph.
Auto-gradedgraphdirected-graphcycle-detectiontopological-sortA robot in an unseen grid senses neighbours for free and pays per move. Find a shortest route to a target, or return to the start and report failure if none exists.
Model answerbfsgridonline-explorationshortest-pathTwo linked tasks: list the nodes visible from the right of a binary tree, and delete the fewest parentheses from a string so the rest is balanced.
Model answerbinary-treebfsstackstringsDecide whether a weighted directed graph has a negative-total cycle reachable from a given source vertex, using Bellman-Ford while ignoring unreachable regions.
Auto-gradedgraphsbellman-fordnegative-cycleshortest-pathStream the leaf strings of a binary tree in left-to-right order and decide whether a pattern occurs in their concatenation, without ever building the full text.
Auto-gradedbinary-treekmpstring-matchingiterative-dfsDerive precedence constraints between letters from adjacent sorted words, then topologically sort them while detecting contradictions and invalid prefix orderings.
Auto-gradedtopological-sortgraphstringscycle-detectionCompute the height of a binary tree given as a child-index table, handling empty input and very deep skewed trees without recursion failures.
Auto-gradedbinary-treedfsiterative-traversaltree-heightCount connected groups of land cells on a grid whose opposite edges wrap around, testing grid traversal with modular adjacency.
Auto-gradedgraphdfsgridunion-findTwo short coding tasks: suggest products that a user's friends bought but the user hasn't, and decide whether one URL can reach another through recorded navigation steps.
Model answerhash-setset-differencegraphbfsProduce a deterministic topological ordering of nodes from prerequisite pairs, or an empty list if a cycle makes it impossible. Tests graph modeling and Kahn's algorithm.
Auto-gradedtopological-sortgraphbfscycle-detectionEvaluate a target variable from a list of assignment strings, growing from simple aliasing to signed sums of variables and integers, then detect circular dependencies safely.
Model answerparsingmemoizationdfscycle-detectionRun a breadth-first traversal over a directed graph of folders, emitting each item once in first-seen order, while validating every reference and handling cycles.
Auto-gradedbfsgraphhash-setdeduplicationCompute a deterministic level-ordered listing of every page reachable from a start page, where failed pages yield no links and concurrency limits must not change the result.
Auto-gradedbfsgraphlevel-ordersetsRun a breadth-first search on a directed or undirected graph, skipping closed nodes, to get each node's minimum edge count from the start, or -1 if it cannot be reached.
Auto-gradedbfsgraphshortest-pathreachabilityDetect structural loops in both a next-pointer chain using constant space and a directed graph reachable from a given entry point.
Auto-gradedtrees-graphslinked-listsdfstwo-pointers