KatInterview

practice a little every day — you've got this ♡

Trees & Graphs Interview Problems

Practice 5967 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.

5967
Problems
2210
Auto-graded
15
Topics
272
Added today

Showing 1–30 of 349 results

  • Trees & GraphsMediumAuto-graded
    Stop Listing and Indented Reporting Tree from Raw Inputs

    Parse a JSON string into formatted stop lines and turn employee rows into a sorted, indented reporting-tree outline using an adjacency list and iterative DFS.

    Trees & Graphsjsontreedfssorting
  • Trees & GraphsMediumAuto-graded
    Smallest-First Course Ordering With Cycle Check

    Produce the lexicographically smallest valid course order from prerequisite pairs, or an empty list if a cycle makes it impossible, using a topological sort with a min-heap.

    Trees & Graphstopological-sortgraphheapcycle-detection
  • Trees & GraphsMediumAuto-graded
    Minimum Grid Manhattan Distance Between Target Characters

    Find the minimum Manhattan distance between any pair of target characters in a 2D character grid.

    Trees & Graphsbfsmatrixdistance-transformgraphs
  • Trees & GraphsHardAuto-graded
    Dictionary Words Hidden in a Letter Grid

    Find every dictionary word that can be traced through adjacent, non-repeating cells of a letter grid, which rewards sharing prefixes across words instead of searching per word.

    Trees & Graphstriebacktrackingdfsgrid
  • Trees & GraphsMediumAuto-graded
    Minimum Hops Between Two Tasks in a Dependency Graph

    Find the fewest directed edges from a start node to a goal node in an acyclic graph, returning 0 for identical nodes and -1 when unreachable; tests BFS on unit-weight graphs.

    Trees & Graphsgraphbfsshortest-pathdag
  • Trees & GraphsMediumAuto-graded
    Collect Allowed Values Depth by Depth from a Nested-List Tree

    Traverse a binary tree given as nested lists level by level and keep only values from an allowed set, preserving left-to-right order and empty levels.

    Trees & Graphsbinary-treelevel-orderbfsstack
  • Trees & GraphsMediumAuto-graded
    Group Staff Names by Reporting Depth

    Turn flat employee-manager records into a list of name groups by depth from the top manager, using a level-by-level breadth-first traversal.

    Trees & Graphsbfstreehash-maplevel-order
  • Trees & GraphsMediumAuto-graded
    Web Crawling Discovery and Bracket Syntax Check

    Perform a depth-bounded graph traversal over web pages based on domain filtering, and validate balanced brackets using a stack.

    Trees & Graphsbfsgraph-traversalstackstring-validation
  • Trees & GraphsMediumAuto-graded
    Fast Power Residue Plus Nearest-Source Grid Map

    Combine square-and-multiply modular exponentiation with a multi-source BFS that labels every grid cell with its distance to the nearest source cell.

    Trees & Graphsbfsmodular-exponentiationgridmulti-source-bfs
  • Trees & GraphsMediumAuto-graded
    Merge Accounts Into Groups via Weighted Field Matching

    Group records into clusters where records are linked when their name/email match score reaches a cutoff, using union-find and bucketing on exact field values.

    Trees & Graphsunion-findconnected-componentsgraphhashing
  • Trees & GraphsMediumAuto-graded
    Anticlockwise Perimeter Walk of an Index-Based Binary Tree

    Compute the anticlockwise perimeter of a binary tree given as child-index arrays, combining root, left edge, leaves and right edge without duplicates.

    Trees & Graphsbinary-treetree-traversaldfsiterative
  • Trees & GraphsMediumAuto-graded
    Smallest-ID Study Order for One Goal Course

    Restrict a dependency graph to the ancestors of one goal node, then produce the smallest-ID-first topological order, or an empty list if a cycle blocks it.

    Trees & Graphstopological-sortgraphheapcycle-detection
  • Trees & GraphsMediumAuto-graded
    Landmass Tally: Count Regions and Find the Biggest

    Count 4-connected groups of land cells in a binary grid and report the area of the largest group, testing graph traversal with correct visited-state handling.

    Trees & Graphsgraphdfsbfsgrid
  • Trees & GraphsMediumAuto-graded
    Shortest Word Chain Allowing One or Two Letter Swaps

    Find a shortest word chain where each step swaps one or two letters, with a fixed tie-break, using BFS on an implicit graph and wildcard bucketing for fast neighbor lookup.

    Trees & Graphsbfsshortest-pathstringshashing
  • Trees & GraphsMediumAuto-graded
    Minimum Rides Across Circular Shuttle Lines

    Find the fewest shuttle lines to board to get from one stop to another, using a graph search over lines and a stop-to-lines index to stay linear in total stops.

    Trees & Graphsbfsgraphhash-mapshortest-path
  • Trees & GraphsMediumAuto-graded
    Edge Silhouettes of a Level-Order Binary Tree

    Build a binary tree from a level-order list with gaps and report the first and last node of every level, testing BFS level separation and level-order parsing.

    Trees & Graphsbinary-treebfslevel-ordertree-construction
  • Trees & GraphsMediumAuto-graded
    Fewest Rolls to Park a Sliding Ball in a Grid

    Model a sliding ball as BFS over resting positions, where each edge is a full roll until a wall or border, and the target counts only if the ball stops on it.

    Trees & Graphsbfsgridgraphshortest-path
  • Trees & GraphsEasyAuto-graded
    Verify Root Sequence Match in N-ary Tree

    Determine if a given target sequence matches a top-down path in an N-ary tree represented by nested dictionaries.

    Trees & Graphstrees-graphsrecursiondfstree-traversal
  • Trees & GraphsMediumAuto-graded
    Counting Separate Land Patches in a 0/1 Grid

    Count connected components of 1-cells in a binary grid using only up/down/left/right adjacency, via flood fill or union-find.

    Trees & Graphsgraphbfsconnected-componentsmatrix
  • Trees & GraphsMediumAuto-graded
    Smallest-Label Build Sequence with Cycle Check

    Produce the lexicographically smallest valid ordering of a dependency graph using a min-heap variant of Kahn's algorithm, returning an empty list if a cycle makes ordering impossible.

    Trees & Graphstopological-sortheapgraphcycle-detection
  • Trees & GraphsMediumAuto-graded
    Collect Path-Tagged Errors from a Nested Payload Schema Check

    Recursively walk a JSON-like value alongside a simplified schema and report type and missing-key errors with exact paths, in deterministic order.

    Trees & Graphsrecursiondfsjsonvalidation
  • Trees & GraphsMediumAuto-graded
    Count Node Changes Between Two Keyed Hierarchies

    Compare two keyed trees in parallel and count changed nodes, where a changed value replaces its whole subtree and unmatched nodes count with their subtrees.

    Trees & Graphstreedfshash-mapsubtree-size
  • Trees & GraphsMediumAuto-graded
    Extract Text From Matching Elements in Messy Markup

    Parse a forgiving HTML string into an element tree in one pass, then collect each matching element's combined text in document order using an iterative depth-first traversal.

    Trees & Graphsparsingtreedfsstack
  • Trees & GraphsMediumAuto-graded
    Total Value of Bit-Strings Along a Tree's Leaf Paths

    Decode a level-order bit tree with skipped None slots, then sum the binary values read along each root-to-leaf path, testing BFS parsing and DFS with a running accumulator.

    Trees & Graphsbinary-treedfsbfslevel-order-parsing
  • Trees & GraphsMediumAuto-graded
    Closest-Pair Dispatch of Couriers on a Walled Grid

    Match couriers to parcels by repeatedly choosing the globally closest pair, using BFS shortest paths on a grid with walls and deterministic tie-breaking.

    Trees & Graphsbfsgridgreedysorting
  • Trees & GraphsMediumAuto-graded
    Find the k-th Value in a Level-Order Encoded Tree's Inorder Sequence

    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.

    Trees & Graphsbinary-treeinorder-traversaliterative-traversalstack
  • Trees & GraphsMediumAuto-graded
    Inherited Permissions Across a Group Hierarchy

    Answer permission queries over a DAG of groups where each group inherits from its parents, using graph traversal with memoization or caching.

    Trees & Graphsgraphdagdfsmemoization
  • Trees & GraphsMediumAuto-graded
    Best Hub-and-Spokes Value Sum with a Spoke Limit

    Choose a hub node and up to k adjacent nodes to maximize the total node value, which tests per-node greedy selection over adjacency lists.

    Trees & Graphsgraphgreedysortingadjacency-list
  • Trees & GraphsMediumAuto-graded
    Reach the Goal State While Avoiding Forbidden Nodes

    Decide 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.

    Trees & Graphsgraphbfsreachabilitydirected-graph
  • Trees & GraphsMediumAuto-graded
    First Moment a Cable Network Becomes One Piece

    Replay 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.

    Trees & Graphsunion-findgraph-connectivitysortingkruskal