KatInterview

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

Trees & Graphs Interview Problems

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

5971
Problems
2211
Auto-graded
15
Topics
272
Added today

Showing 91–120 of 349 results

  • Trees & GraphsMediumAuto-graded
    Longest Chain of Accounts Reachable Through Follows

    Find the longest simple path from a start node in a directed graph with possible cycles, using backtracking DFS with a per-path visited set.

    Trees & Graphsgraphdfsrecursionbacktracking
  • Trees & GraphsMediumAuto-graded
    Five-Task Toolkit: Graphs, Swaps, Digits, Cache, Trees

    Solve five small tasks in one call: count graph components, minimum swaps to sort, largest monotonic number, simulate an LRU cache, and compare two N-ary trees.

    Trees & Graphsgraph-traversalcycle-decompositiongreedy-digitslru-cache
  • Trees & GraphsMediumAuto-graded
    Tally Separate Clusters of Ones in a Binary Matrix

    Count 4-directionally connected clusters of 1s in a binary matrix using graph traversal, with care for visited tracking, empty inputs, and deep clusters.

    Trees & Graphsdfsgridconnected-componentsmatrix
  • Trees & GraphsHardAuto-graded
    Shortest Looping Command Script for a Fuel-Limited Rover

    Brute-force enumerate looping command scripts of length 1 to 10 and simulate each on a grid with fuel, walls and refuel pads, using state-repeat detection to stop loops.

    Trees & Graphssimulationbrute-forcegridcycle-detection
  • Trees & GraphsMediumAuto-graded
    Rebuild a Nested Menu Tree from Indented Text

    Parse an indentation-based, pipe-delimited menu into a nested tree of dictionaries, testing stack-based hierarchy reconstruction and careful field parsing.

    Trees & Graphstree-constructionstackparsingstrings
  • Trees & GraphsEasyAuto-graded
    Nearest Node in a Binary Search Tree

    Find the value in a Binary Search Tree represented as a level-order array that has the smallest absolute difference to a target number.

    Trees & Graphstrees-graphsbinary-search-treesearch
  • Trees & GraphsMediumAuto-graded
    Fewest Moves Through a Walled Maze

    Compute the minimum number of 4-directional moves between two open cells of a binary maze, or -1 if unreachable; tests breadth-first search on an implicit grid graph.

    Trees & Graphsbfsgridshortest-pathgraph
  • Trees & GraphsMediumAuto-graded
    Validate Binary Tree from Directed Edge List

    Evaluates tree properties, cycle detection, edge validity, and in-order traversal under strict error precedence.

    Trees & Graphstreesgraphsdfstraversal
  • Trees & GraphsMediumAuto-graded
    Prune a Branch from a Parent-Array Forest

    Remove a node and its entire subtree from a forest stored as a parent array, either by marking slots with -1 or by compacting indices and remapping parent pointers.

    Trees & Graphsforestdfsparent-arrayreindexing
  • Trees & GraphsMediumAuto-graded
    Deterministic Mine Grid Plus Letter-Path Check

    Build a mine-count grid with mines in row-major order, then use DFS backtracking to check whether a word can be traced through a separate letter grid.

    Trees & Graphsgridbacktrackingdfssimulation
  • Trees & GraphsMediumAuto-graded
    Tally Separate Clusters of Occupied Cells in a Grid

    Count groups of orthogonally touching '1' cells in a character grid, a connected-components task solved with an iterative flood fill or union-find.

    Trees & Graphsgraphbfsgridconnected-components
  • Trees & GraphsMediumAuto-graded
    Find Minimum Network Distance Between Users

    Compute the shortest path distance between two nodes in an unweighted undirected graph using Breadth-First Search.

    Trees & Graphsbfsgraphshortest-pathbreadth-first-search
  • Trees & GraphsMediumAuto-graded
    Reconstruct Alphabet Order from Sorted Vocabulary

    Determine character precedence in an unfamiliar alphabet using a sorted list of terms, returning the lexicographically first valid character sequence or an empty string for invalid inputs.

    Trees & Graphstrees-graphsheaps-sortingstrings
  • Trees & GraphsMediumAuto-graded
    Fewest Removals to Cap a Rooted Tree's Height

    Count the minimum non-root nodes to remove from a parent-array tree, splicing children upward, so the height is at most a limit; tests subtree heights and a path-decomposition argument.

    Trees & Graphstreesubtree-heightiterative-bfsgreedy
  • Trees & GraphsEasyAuto-graded
    Prune a Labeled Tree by Forbidden Substring

    Recursively copy an n-ary tree while dropping every node whose label contains a given substring, together with its subtree, without mutating the input.

    Trees & Graphstreerecursiondfsstrings
  • Trees & GraphsMediumAuto-graded
    Players Whose Leaderboard Position Is Forced

    Find every player comparable to all others in a consistent win/loss DAG, using transitive reachability in both directions, ideally with bitsets.

    Trees & Graphsgraphtopological-sorttransitive-closurebitset
  • Trees & GraphsMediumAuto-graded
    Smallest Valid Task Schedule with Prerequisites

    Produce the lexicographically smallest topological ordering of a directed graph, or report that no ordering exists because of a cycle.

    Trees & Graphstopological-sortheapgraphcycle-detection
  • Trees & GraphsMediumAuto-graded
    Tree Height and Peak Concurrent Bookings

    Compute the height of an index-encoded binary tree without deep recursion, and the minimum rooms needed for half-open time intervals, returned together as a pair.

    Trees & Graphsbinary-treeiterative-dfsintervalssweep-line
  • Trees & GraphsEasyAuto-graded
    Find Tree K-th Ancestor for Queries

    Answer k-th ancestor queries on a rooted tree efficiently using binary lifting.

    Trees & Graphstrees-graphsmath-bitsarrays
  • Trees & GraphsMediumAuto-graded
    Count Clusters of Linked Playlists from a Match Grid

    Count connected components in an undirected graph given as an n x n adjacency matrix, where links chain together transitively.

    Trees & Graphsgraphconnected-componentsdfsunion-find
  • Trees & GraphsMediumAuto-graded
    Find the K-th Smallest Element in a BST

    Find the k-th smallest key in a binary search tree represented as a level-order array.

    Trees & Graphstrees-graphstreesin-order-traversalbinary-search-tree
  • Trees & GraphsMediumAuto-graded
    Trace a Phrase Through Neighboring Tiles

    Decide whether a string can be traced through a rectangular letter grid by stepping to up/down/left/right neighbors without reusing a cell, using backtracking search.

    Trees & Graphsbacktrackingdfsmatrixpruning
  • Trees & GraphsMediumAuto-graded
    Deepest Shared Ancestor of Two Nodes in a Rooted Tree

    Find the deepest node whose subtree contains two given nodes in a tree stored as child lists, using a single depth-first pass that stays safe on very deep trees.

    Trees & Graphstreedfslowest-common-ancestorpost-order
  • Trees & GraphsMediumAuto-graded
    Cheapest Transit Pick on a Lettered Street Grid

    Run a per-mode BFS on a grid to find step counts, then choose the mode with least total time, then least total cost, then smallest index.

    Trees & Graphsbfsgridshortest-pathtie-breaking
  • Trees & GraphsMediumAuto-graded
    Determine Build Task Schedule

    Find a valid topological sequence for tasks with prerequisite constraints or detect cyclic dependencies.

    Trees & Graphsgraphtopological-sortbfsdfs
  • Trees & GraphsMediumAuto-graded
    Tree Span and Grid Escape: Two Graph Queries in One Call

    Compute a tree's longest path in edges and the shortest 4-directional route through a grid with obstacles, returning both results together.

    Trees & Graphsbfstree-diametershortest-pathgrid
  • Trees & GraphsMediumAuto-graded
    Count Islands of Mutual Acquaintances in a Matrix

    Count connected components in an undirected graph given as a symmetric adjacency matrix, without modifying the input, using graph traversal or union-find.

    Trees & Graphsgraphconnected-componentsdfsunion-find
  • Trees & GraphsEasyAuto-graded
    Navigate Web Server Maze to Target Node

    Evaluates breadth-first graph search and set tracking to traverse a network of web pages and find an exit target.

    Trees & Graphstrees-graphsbreadth-first-searchhash-map
  • Trees & GraphsMediumAuto-graded
    Emptying a Directory Tree: Record the Deletion Order

    Produce the deterministic bottom-up deletion sequence for a file tree given as flat path lists, using an iterative post-order traversal that survives very deep nesting.

    Trees & Graphspost-orderdfsiterativestack
  • Trees & GraphsMediumAuto-graded
    Assemble a Reply Thread Forest from Flat Records

    Convert an unordered flat list of records with parent links into a nested forest, preserving input order and rejecting duplicate ids, missing parents and cycles.

    Trees & Graphstree-constructionhash-mapcycle-detectionforest