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 61–90 of 434 results
Simulate one Minesweeper tap on a grid, flood-revealing connected zero-count cells with an iterative traversal and labelling border cells with their 8-neighbour mine counts.
Auto-gradedbfsflood-fillgridmatrixFind the largest edge count of a triangle-free simple graph on n vertices and output a canonical extremal graph, testing Mantel's theorem and the complete bipartite construction.
Auto-gradedgraph-theoryconstructionbipartitemantel-theoremCompare two keyed N-ary trees given as nested dicts and count nodes that were added, removed, given a new value, or attached to a different parent.
Auto-gradedtreehash-mapdfstree-diffDecide whether tasks with prerequisite constraints can all be run, and if so produce the lexicographically smallest valid run order, using topological sorting with a min-heap.
Auto-gradedtopological-sortgraphheapcycle-detectionBuild an undirected graph from friendship pairs, then rank every non-friend of a target user by mutual friends, degree and ID using a composite sort key.
Auto-gradedgraphadjacency-listsortingsetsProduce a deterministic course order that respects prerequisite pairs using a queue-based topological sort, or an empty list if the prerequisites contain a cycle.
Auto-gradedtopological-sortgraphbfscycle-detectionFind the deepest node that is an ancestor of two given values in a binary tree stored as a heap-indexed array, using a parent map and ancestor walk.
Auto-gradedbinary-treelowest-common-ancestorbfshash-mapDetermine a valid sequential execution order for a set of tasks given dependency rules, or detect impossible circular dependencies.
Auto-gradedtopological sortgraphbfsdegree countSolve two fundamental interview tasks: identifying the rightmost nodes of a binary tree by level and finding non-duplicate zero-sum triplets in an un-ordered array.
Model answerbinary-treebfstwo-pointerssortingCount, for each node of a weighted tree, the unordered pairs of other nodes at distances divisible by k and lying in different branches around that node.
Auto-gradedtreedfscountingcombinatoricsCount groups of touching 1-cells in a binary grid where all eight surrounding cells count as neighbors, testing grid traversal and connected-component counting.
Auto-gradedgridbfsconnected-componentsmatrixCompute distances in a tree from three marked vertices and count the other vertices whose three distances form a right-triangle (Pythagorean) triple.
Auto-gradedbfstreegraph-traversalmathGuide a robot through a map it can only discover step by step, reaching the target with no repeated visits, real backtracking, and a clear result when the target is unreachable.
Model answerdfsbacktrackinggraph-traversalgridFind the one node in a directed relation that everyone points to and that points to nobody, using a linear elimination pass followed by verification.
Auto-gradedgraphcelebrity-problemtwo-passeliminationFind the lexicographically smallest valid ordering of nodes under child-before-parent and extra precedence rules, or report impossibility when the rules contradict.
Auto-gradedtopological-sortheapcycle-detectiongraphCompute the minimum of every K x K window in a grid, then order recipes so that every derived ingredient is made before the dish that uses it, detecting cycles.
Model answertopological-sortmonotonic-dequesliding-windowkahns-algorithmProduce a deterministic topological ordering of courses from prerequisite pairs, or an empty list when a circular requirement makes it impossible.
Auto-gradedtopological-sortgraphbfscycle-detectionGiven directed currency exchange quotes, decide whether some round trip returns more than it started with, and output one such round trip as an ordered list of currencies.
Model answergraphbellman-fordnegative-cyclefloating-pointFind the smallest start index in a jump array whose walk never goes below index 0, handling cycles and right exits, ideally in linear time.
Auto-gradedfunctional-graphcycle-detectionmemoizationarraysFind the unique path between two pre-order-indexed nodes in a huge recursively defined tree, using subtree sizes to navigate without building it.
Auto-gradedtreelcarecursionpre-orderFind the route between two nodes in an undirected graph that maximizes the product of per-link delivery probabilities, using a Dijkstra-style search with a max-priority queue.
Auto-gradedgraphdijkstraheapshortest-pathBuild a height-balanced BST from a sorted list using a fixed middle-element rule, then serialize it level by level with null placeholders.
Auto-gradedbinary-search-treedivide-and-conquerrecursionlevel-order-traversalFind the fewest moves through a grid where lowercase cells give keys and uppercase cells are gates, using BFS over position plus the set of collected keys.
Auto-gradedbfsgridstate-space-searchkeys-and-doorsReassemble an unordered collection of trace span objects into a forest of child-linked execution trees sorted chronologically.
Auto-gradedtreeshash-tablesortinggraphConstruct an adjacency graph from reporting links and perform a level-order traversal to group node IDs by depth.
Auto-gradedtrees-graphsbfshash-mapFind the largest connected land region achievable by turning at most one water cell into land, which tests component labeling and merging neighbors without double counting.
Auto-gradedgridflood-fillconnected-componentsdfsDecide whether a set of people can be placed into two teams so that no rival pair shares a team, which tests graph 2-coloring across possibly disconnected components.
Auto-gradedgraphbipartitebfscoloringTraverse a nested dictionary tree in pre-order, tracking depth to build indented lines with a fixed marker, preserving the given child order.
Auto-gradedtree-traversaldfsstackstringsDecide whether a set of ordering rules between numbered modules contains a circular dependency, by detecting a cycle in a directed graph.
Auto-gradedgraphtopological-sortcycle-detectionbfsDetermine the shortest path distance between two nodes in an unweighted, undirected graph using Breadth-First Search.
Auto-gradedbfsgraphshortest-pathhash-map