KatInterview

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-graded
    Indented 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-graded
    Split 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-graded
    Distance 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-graded
    Fastest 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-graded
    Sort 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-graded
    Split 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-graded
    Fastest 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-graded
    Distinct 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-graded
    Does 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-graded
    Negative 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-graded
    Find 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-graded
    Recover 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-graded
    Tallest 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-graded
    Counting 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-graded
    Unlock 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-graded
    Gather 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-graded
    Level-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-graded
    Fewest 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-graded
    Identify 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-graded
    Minesweeper 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-graded
    Densest 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-graded
    Diff 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-graded
    Lexicographically 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-graded
    Rank 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-graded
    Schedule 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-graded
    Nearest 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-graded
    Schedule 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-graded
    Hub 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-graded
    Cluster 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-graded
    Right-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