KatInterview

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

Trees & Graphs Interview Problems

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

5972
Problems
2212
Auto-graded
15
Topics
273
Added today

Showing 121–150 of 434 results

  • 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 & GraphsMediumModel answer
    Feature Flag Rollout Validator with Dependency Cycle Detection

    Parse feature dependency specs, run modular checksum verification, and evaluate user eligibility and feature graph dependencies while handling cyclic rollouts.

    Trees & Graphstrees-graphsstringsdepth-first-search
  • Trees & GraphsHardModel answer
    Topological Task Scheduler and Strict Expression Parser

    Solve a task dependency ordering problem via topological sort and build an expression evaluator supporting custom operator precedence, unary signs, and overflow checks.

    Trees & Graphstopological-sortgraph-algorithmsexpression-parserstacks
  • 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 & GraphsHardModel answer
    Evaluate Chained Sum Definitions with Error Detection

    Compute the integer value of every variable defined as a sum of literals and other variables, raising distinct errors for duplicate, undefined, or circular definitions.

    Trees & Graphstopological-sortgraphkahn-algorithmcycle-detection
  • 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 & GraphsMediumModel answer
    Repairing a Search Tree After One Pair of Keys Trades Places

    Two keys in a binary search tree were exchanged by mistake. Restore ordering by swapping values back, keeping the shape intact, in linear time and constant extra space.

    Trees & Graphsbinary-search-treemorris-traversalin-ordertree-recovery
  • 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
  • Trees & GraphsEasyAuto-graded
    Label Each Node as Root, Inner, or Leaf from Parent Links

    Classify every node of a rooted tree given as [id, parent_id] pairs into Root, Inner, or Leaf, using a set of parent ids, and return the labels sorted by id.

    Trees & Graphstreehash-setclassificationsorting
  • Trees & GraphsMediumModel answer
    Minimum Additions to Equalize Every Subtree Sum

    Calculate the minimum non-negative value additions needed so that the node sum of every subtree in a binary tree equals K, returning -1 if impossible.

    Trees & Graphstrees-graphspost-ordergreedydepth-first-search
  • Trees & GraphsMediumAuto-graded
    Reveal Cells on a Mine Grid After a Tap

    Simulate one tap on a minesweeper grid, using a flood fill from zero-count cells to reveal connected safe regions and their numbered borders.

    Trees & Graphsbfsflood-fillgridsimulation
  • Trees & GraphsMediumModel answer
    Same-Host Link Crawler: Sequential and Concurrent Versions

    Crawl every page reachable from a seed URL on the same host, ignoring URL fragments, first with one thread and then with a thread-safe parallel design.

    Trees & Graphsbfsgraph-traversalconcurrencythread-pool