KatInterview

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

Trees & Graphs Interview Problems

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

5973
Problems
2212
Auto-graded
15
Topics
271
Added today

Showing 121–150 of 349 results

  • 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 & 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 & GraphsMediumAuto-graded
    Follow Graph: Following Lists and Mutual-Count Suggestions

    Answer batched queries on a static directed follow graph: list direct followees and rank second-hop accounts by how many followees also follow them.

    Trees & Graphsgraphhash-mapadjacency-listsorting
  • Trees & GraphsMediumAuto-graded
    Column-by-Column Readout of a Binary Tree from Level-Order Input

    Group binary tree nodes by horizontal column, ordering each column by depth and then by value, with the tree given as a level-order list with nulls.

    Trees & Graphsbinary-treebfssortingcoordinates
  • Trees & GraphsMediumAuto-graded
    Cheapest Dungeon Escape Through Monster Cells

    Find the minimum number of monster cells entered on any route from start to exit in a grid with walls, using a weighted shortest-path search such as Dijkstra or 0-1 BFS.

    Trees & Graphsgriddijkstrashortest-path0-1-bfs
  • Trees & GraphsHardAuto-graded
    Most Distinct Values After Shifting Each Element

    Choose one shift per element from a given list to maximize distinct results; tests recognizing the problem as bipartite matching and implementing Hopcroft-Karp efficiently.

    Trees & Graphsbipartite-matchinghopcroft-karpgraphsgreedy-pitfalls
  • Trees & GraphsEasyAuto-graded
    Collect Every File Path from a Nested Directory Tree

    Traverse a nested directory dictionary depth-first and build the full slash-joined path for each file, following precise joining and ordering rules.

    Trees & Graphsdfstree-traversalrecursionstrings
  • Trees & GraphsMediumAuto-graded
    Group Binary Tree Nodes into Vertical Columns

    Group nodes of a binary tree represented in array heap layout into vertical columns ordered from left to right.

    Trees & Graphstrees-graphsbreadth-first-searchbinary-treequeues
  • Trees & GraphsMediumAuto-graded
    Count Separate Land Masses in a Grid

    Evaluate grid graph connectivity by finding the number of distinct 4-directionally connected land components.

    Trees & Graphstrees-graphsdfs-bfsgrid
  • Trees & GraphsMediumAuto-graded
    Spreading Pop Wave Across a Square Balloon Grid

    Compute the earliest time each cell of a square grid is reached by a wave spreading from several seed cells, using multi-source breadth-first search.

    Trees & Graphsbfsgridmulti-sourceshortest-path
  • Trees & GraphsMediumAuto-graded
    Verify Root-to-Leaf Sequence in Binary Tree

    Check if a list of integers forms a complete valid root-to-leaf path in a binary tree given as an adjacency table.

    Trees & Graphstrees-graphstreearray
  • Trees & GraphsMediumAuto-graded
    Merge Per-User Action Sequences into a Counted Prefix Tree

    Group event records by user, order each user's events by time, merge the sequences into a prefix tree with distinct-user counts, and print it in a fixed preorder format.

    Trees & Graphstriehash-mapstable-sortdfs
  • Trees & GraphsMediumAuto-graded
    Perform Connected Component Recolor on Grid

    Perform a flood-fill recoloring on a 2D integer grid while avoiding call stack overflow and preserving the original input.

    Trees & Graphsbfs-dfsmatrixflood-fillgraph-traversal
  • Trees & GraphsMediumAuto-graded
    Evaluate Variable Expressions with Dependencies

    Evaluate symbolic variable definitions with standard arithmetic precedence, resolving variable dependencies while detecting cycles and missing definitions.

    Trees & Graphstrees-graphsstacks-queuestopological-sortrecursion
  • Trees & GraphsEasyAuto-graded
    Largest Cluster of Two-Digit Numbers Linked by Common Digits

    Find the largest connected cluster in a list of two-digit numbers where links come from shared digits, testing graph connectivity modeling with union-find or digit-hub nodes.

    Trees & Graphsunion-findconnected-componentsbitmaskgraph
  • Trees & GraphsMediumAuto-graded
    Shortest Grid Path to Nearest Fulfillment Hub

    Find the minimum travel distance from every destination point to its closest source hub on a 2D grid using multi-source Breadth-First Search.

    Trees & GraphsBFSMulti-Source BFSGrid TraversalShortest Path
  • Trees & GraphsMediumAuto-graded
    Minimum Steps Across a Square Maze with Diagonal Moves

    Tests breadth-first search on a square binary grid with eight-directional moves, handling blocked endpoints, visited tracking, and counting cells along the shortest route.

    Trees & Graphsbfsgridshortest-pathgraph-traversal
  • Trees & GraphsMediumAuto-graded
    Sparse Vector Overlap Sum and Land Cluster Counter

    Combine a merge-style sparse vector dot product with an iterative flood fill that counts 4-connected land clusters, returning both results together.

    Trees & Graphstwo-pointerssparse-vectorgriddfs
  • Trees & GraphsMediumAuto-graded
    Minimum Web Page Hop Distance

    Find the shortest distance between two web pages in a directed link graph using breadth-first search.

    Trees & Graphsbfsgraphshortest-pathtrees-graphs
  • Trees & GraphsMediumAuto-graded
    Grid Land Mass Valuation

    Find all orthogonal connected components of non-zero cells in a 2D grid and calculate the total sum of cell values for each component.

    Trees & Graphstrees-graphsbreadth-first-searchdepth-first-searchmatrix
  • Trees & GraphsMediumAuto-graded
    Sum of Keys Within a Value Window in a Serialized BST

    Rebuild a BST from a level-order list and sum the node values inside an inclusive range, using ordering to skip subtrees that cannot contain qualifying values.

    Trees & Graphsbinary-search-treedfstree-constructionpruning
  • Trees & GraphsHardAuto-graded
    Climbing Routes From the Corner: Batch Lookup of Shortest Paths

    Run one BFS from the top-left cell of a grid with distinct values, using strictly increasing moves, then answer many target queries by rebuilding paths from stored parent links.

    Trees & Graphsbfsgridshortest-pathpath-reconstruction
  • Trees & GraphsMediumAuto-graded
    Hide Flagged Comment Threads for a Pet-Preference Reader

    Find all comments hidden from a reader when any flagged comment hides its whole reply subtree, handling unordered input, dangling parent IDs, and very deep chains.

    Trees & Graphstreedfshash-mapforest
  • Trees & GraphsMediumAuto-graded
    Alphabetical Job Scheduler with Prerequisite Rules

    Produce a full run order for jobs under prerequisite constraints, always picking the alphabetically smallest available job, or return an empty list if a cycle exists.

    Trees & Graphstopological-sortheapgraphkahn-algorithm
  • Trees & GraphsMediumAuto-graded
    Vertical Column Grouping of Array-Represented Binary Trees

    Organize the elements of a binary tree into vertical columns using level-order traversal rules on a level-order array input.

    Trees & Graphstrees-graphsbreadth-first-searcharraysqueues
  • Trees & GraphsMediumAuto-graded
    Find k-th Subordinate in Cascade Traversal

    Find the k-th node reached during a preorder depth-first traversal of a specific node's subtree when children are visited in ascending numerical order.

    Trees & Graphstrees-graphsdfstree-traversal
  • Trees & GraphsEasyAuto-graded
    Trace Path to Terminal Node or Cycle Entry

    Determine the final visited node when following single-successor directed edges until reaching a dead end or a previously visited node.

    Trees & Graphstrees-graphsarrayshash-table
  • Trees & GraphsMediumAuto-graded
    Can Every Task Be Scheduled Given Dependency Pairs?

    Tests modeling dependencies as a directed graph and detecting whether a cycle prevents a complete valid ordering, e.g. via indegree-based topological sorting.

    Trees & Graphsgraphtopological-sortcycle-detectionbfs
  • Trees & GraphsHardAuto-graded
    Calculate Subtree Totals in a Tree

    Given an undirected tree and node weights, compute the sum of values for every subtree rooted at node 1.

    Trees & Graphstrees-graphsdfstree-traversalbottom-up
  • Trees & GraphsEasyAuto-graded
    Extract Sorted Values from Level-Order Encoded BST

    Extract all valid integer elements from a level-order serialized binary search tree and return them in non-decreasing order.

    Trees & Graphstrees-graphsheaps-sortingbinary-search-tree