KatInterview

Topic

Trees & Graphs interview problems

Traversals, shortest paths, topological order and union-find. 434 original problems, each with hidden test cases you can run in the browser.

Problems434

25 easy362 medium47 hard

Showing 1–30 of 434 results

  • Compute all possible configurations of n non-attacking queens on an n x n board using stateful depth-first search.

    Model answerbacktrackingrecursiondepth-first-search
  • 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.

    Auto-gradedjsontreedfssorting
  • 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.

    Auto-gradedtopological-sortgraphheapcycle-detection
  • Find the minimum Manhattan distance between any pair of target characters in a 2D character grid.

    Auto-gradedbfsmatrixdistance-transformgraphs
  • Construct a valid contiguous layout for multiple crop zones on a grid and find the shortest path avoiding edge-touching zones.

    Model answertrees-graphsgridbfsconstructive-algorithms
  • 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.

    Auto-gradedtriebacktrackingdfsgrid
  • Implement topological sort, BFS, DFS and Dijkstra on a dependency graph, then discuss CSR versus adjacency lists, CPU/GPU parallel traversal, and deterministic testing.

    Model answertopological-sortbfs-dfsdijkstracsr
  • 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.

    Auto-gradedgraphbfsshortest-pathdag
  • 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.

    Auto-gradedbinary-treelevel-orderbfsstack
  • 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.

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

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

    Auto-gradedbfsmodular-exponentiationgridmulti-source-bfs
  • 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.

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

    Auto-gradedbinary-treetree-traversaldfsiterative
  • 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.

    Auto-gradedtopological-sortgraphheapcycle-detection
  • Given a DAG of task dependencies and one start node, compute how many times each node fires when every activation fans out once along every outgoing edge.

    Model answerdagtopological-sortpath-countinggraph-traversal
  • 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.

    Auto-gradedgraphdfsbfsgrid
  • 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.

    Auto-gradedbfsshortest-pathstringshashing
  • 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.

    Auto-gradedbfsgraphhash-mapshortest-path
  • 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.

    Auto-gradedbinary-treebfslevel-ordertree-construction
  • 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.

    Auto-gradedbfsgridgraphshortest-path
  • Reconstruct a binary tree given its inorder and postorder traversal arrays, assuming all node values are unique.

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

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

    Auto-gradedgraphbfsconnected-componentsmatrix
  • 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.

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

    Auto-gradedrecursiondfsjsonvalidation
  • 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.

    Auto-gradedtreedfshash-mapsubtree-size
  • 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.

    Auto-gradedparsingtreedfsstack
  • 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.

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

    Auto-gradedbfsgridgreedysorting

More topics