practice a little every day — you've got this ♡
Trees & Graphs Interview Problems
Practice 5969 original coding, SQL, system design, machine learning and behavioral problems across 15 topics. Coding and SQL problems run against hidden test cases right in your browser; every problem has hints and a worked solution, free.
- 5969
- Problems
- 2211
- Auto-graded
- 15
- Topics
- 272
- Added today
Showing 31–60 of 349 results
- Trees & GraphsMediumAuto-gradedIndented Staff Listing from Direct-Report Pairs
Build a forest from (employee, manager) pairs and output each person with their depth in a sorted depth-first order, handling very deep chains iteratively.
Trees & Graphstreedfsforesthash-map - Trees & GraphsMediumAuto-gradedSplit a Nested Structure into Values and a Shape Template
Implement 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.
Trees & Graphsrecursiondfsnested-structuresdictionaries - Trees & GraphsEasyAuto-gradedDistance Map to the Closest Exit in a Maze Grid
Compute, 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.
Trees & Graphsbfsgridmulti-source-bfsshortest-path - Trees & GraphsMediumAuto-gradedFastest Route Time Through a One-Way Street Network
Find 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.
Trees & Graphsdijkstrashortest-pathgraphheap - Trees & GraphsMediumAuto-gradedSort a List and Replay Commands on a Binary Search Tree
Sort 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.
Trees & Graphsbinary-search-treequicksortrecursiontree-deletion - Trees & GraphsMediumAuto-gradedSplit Nodes Into Two Groups With No Internal Links
Decide 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.
Trees & Graphsgraphbfsbipartiteconnected-components - Trees & GraphsMediumAuto-gradedFastest Signal Arrival Times Across a Relay Grid
Compute shortest-path distances from one source in an undirected graph with non-negative weights, handling parallel edges, self-loops, and unreachable nodes.
Trees & Graphsdijkstrashortest-pathgraphheap - Trees & GraphsHardAuto-gradedDistinct Transitive Prerequisites in a Dependency DAG
For each node in a DAG whose links may name unknown items, count the distinct nodes reachable along link direction, counting shared descendants once.
Trees & Graphsgraphdagtopological-sortbitset - Trees & GraphsMediumAuto-gradedDoes This One-Way Network Contain a Loop?
Decide whether a directed graph given as an adjacency list contains a cycle, handling self-loops, parallel edges, disconnected parts and an empty graph.
Trees & Graphsgraphdirected-graphcycle-detectiontopological-sort - Trees & GraphsMediumAuto-gradedNegative Loop Reachable From a Source Vertex
Decide whether a weighted directed graph has a negative-total cycle reachable from a given source vertex, using Bellman-Ford while ignoring unreachable regions.
Trees & Graphsgraphsbellman-fordnegative-cycleshortest-path - Trees & GraphsMediumAuto-gradedFind a Word Spanning the Fragments at a Tree's Leaves
Stream 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.
Trees & Graphsbinary-treekmpstring-matchingiterative-dfs - Trees & GraphsMediumAuto-gradedRecover a Letter Ordering from a Sorted Word List
Derive precedence constraints between letters from adjacent sorted words, then topologically sort them while detecting contradictions and invalid prefix orderings.
Trees & Graphstopological-sortgraphstringscycle-detection - Trees & GraphsMediumAuto-gradedTallest Chain of Nodes in an Index-Linked Binary Tree
Compute the height of a binary tree given as a child-index table, handling empty input and very deep skewed trees without recursion failures.
Trees & Graphsbinary-treedfsiterative-traversaltree-height - Trees & GraphsMediumAuto-gradedCounting Land Clusters on a Wrap-Around Map
Count connected groups of land cells on a grid whose opposite edges wrap around, testing grid traversal with modular adjacency.
Trees & Graphsgraphdfsgridunion-find - Trees & GraphsMediumAuto-gradedUnlock Order: Build a Dependency-Respecting Study Plan
Produce 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.
Trees & Graphstopological-sortgraphbfscycle-detection - Trees & GraphsMediumAuto-gradedGather Unique Items Across a Linked Collection Graph
Run 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.
Trees & Graphsbfsgraphhash-setdeduplication - Trees & GraphsMediumAuto-gradedLevel-by-Level Site Walk With Failing Pages and a Fetch Cap
Compute 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.
Trees & Graphsbfsgraphlevel-ordersets - Trees & GraphsMediumAuto-gradedFewest Hops from a Start Node While Avoiding Closed Nodes
Run 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.
Trees & Graphsbfsgraphshortest-pathreachability - Trees & GraphsMediumAuto-gradedIdentify Cycles in Linear Chains and Directed Networks
Detect structural loops in both a next-pointer chain using constant space and a directed graph reachable from a given entry point.
Trees & Graphstrees-graphslinked-listsdfstwo-pointers - Trees & GraphsMediumAuto-gradedMinesweeper Tap: Flood-Reveal Safe Cells and Show Mine Counts
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.
Trees & Graphsbfsflood-fillgridmatrix - Trees & GraphsMediumAuto-gradedDensest Graph Without Triangles: Count and Build
Find 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.
Trees & Graphsgraph-theoryconstructionbipartitemantel-theorem - Trees & GraphsMediumAuto-gradedDiff Two Versions of a Keyed Hierarchy
Compare 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.
Trees & Graphstreehash-mapdfstree-diff - Trees & GraphsMediumAuto-gradedLexicographically Smallest Valid Task Schedule
Decide 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.
Trees & Graphstopological-sortgraphheapcycle-detection - Trees & GraphsMediumAuto-gradedRank Suggested Connections by Shared Friends
Build 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.
Trees & Graphsgraphadjacency-listsortingsets - Trees & GraphsMediumAuto-gradedSchedule Courses Under Prerequisite Rules
Produce a deterministic course order that respects prerequisite pairs using a queue-based topological sort, or an empty list if the prerequisites contain a cycle.
Trees & Graphstopological-sortgraphbfscycle-detection - Trees & GraphsEasyAuto-gradedNearest Shared Ancestor in an Array-Encoded Binary Tree
Find 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.
Trees & Graphsbinary-treelowest-common-ancestorbfshash-map - Trees & GraphsHardAuto-gradedSchedule Tasks with Precedence Constraints
Determine a valid sequential execution order for a set of tasks given dependency rules, or detect impossible circular dependencies.
Trees & Graphstopological sortgraphbfsdegree count - Trees & GraphsMediumAuto-gradedHub Node Pair Counting on a Weighted Tree with Divisible Distances
Count, 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.
Trees & Graphstreedfscountingcombinatorics - Trees & GraphsMediumAuto-gradedCluster Counting on a Grid with King-Move Adjacency
Count groups of touching 1-cells in a binary grid where all eight surrounding cells count as neighbors, testing grid traversal and connected-component counting.
Trees & Graphsgridbfsconnected-componentsmatrix - Trees & GraphsMediumAuto-gradedRight-Triangle Distance Vertices in a Tree
Compute distances in a tree from three marked vertices and count the other vertices whose three distances form a right-triangle (Pythagorean) triple.
Trees & Graphsbfstreegraph-traversalmath