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 121–150 of 434 results

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

    Auto-gradedforestdfsparent-arrayreindexing
  • 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.

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

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

    Auto-gradedbfsgraphshortest-pathbreadth-first-search
  • 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.

    Auto-gradedtrees-graphsheaps-sortingstrings
  • 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.

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

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

    Auto-gradedgraphtopological-sorttransitive-closurebitset
  • Produce the lexicographically smallest topological ordering of a directed graph, or report that no ordering exists because of a cycle.

    Auto-gradedtopological-sortheapgraphcycle-detection
  • 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.

    Auto-gradedbinary-treeiterative-dfsintervalssweep-line
  • Answer k-th ancestor queries on a rooted tree efficiently using binary lifting.

    Auto-gradedtrees-graphsmath-bitsarrays
  • Count connected components in an undirected graph given as an n x n adjacency matrix, where links chain together transitively.

    Auto-gradedgraphconnected-componentsdfsunion-find
  • Find the k-th smallest key in a binary search tree represented as a level-order array.

    Auto-gradedtrees-graphstreesin-order-traversalbinary-search-tree
  • 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.

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

    Auto-gradedtreedfslowest-common-ancestorpost-order
  • 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.

    Auto-gradedbfsgridshortest-pathtie-breaking
  • Parse feature dependency specs, run modular checksum verification, and evaluate user eligibility and feature graph dependencies while handling cyclic rollouts.

    Model answertrees-graphsstringsdepth-first-search
  • Solve a task dependency ordering problem via topological sort and build an expression evaluator supporting custom operator precedence, unary signs, and overflow checks.

    Model answertopological-sortgraph-algorithmsexpression-parserstacks
  • Find a valid topological sequence for tasks with prerequisite constraints or detect cyclic dependencies.

    Auto-gradedgraphtopological-sortbfsdfs
  • 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.

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

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

    Auto-gradedgraphconnected-componentsdfsunion-find
  • 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.

    Model answerbinary-search-treemorris-traversalin-ordertree-recovery
  • Evaluates breadth-first graph search and set tracking to traverse a network of web pages and find an exit target.

    Auto-gradedtrees-graphsbreadth-first-searchhash-map
  • 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.

    Auto-gradedpost-orderdfsiterativestack
  • 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.

    Auto-gradedtree-constructionhash-mapcycle-detectionforest
  • 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.

    Auto-gradedtreehash-setclassificationsorting
  • 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.

    Model answertrees-graphspost-ordergreedydepth-first-search
  • Simulate one tap on a minesweeper grid, using a flood fill from zero-count cells to reveal connected safe regions and their numbered borders.

    Auto-gradedbfsflood-fillgridsimulation
  • 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.

    Model answerbfsgraph-traversalconcurrencythread-pool

More topics