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 301–330 of 434 results

  • Tests the ability to model transitive relationships between records using Union-Find (DSU) to identify connected components linked by shared attribute values.

    Auto-gradedunion-findconnected-componentshashing
  • Find the earliest time a traveler can reach a destination station given a fixed train timetable, then reconstruct the full itinerary with transfers and waits.

    Model answergraph-searchdijkstrabfstime-expanded-graph
  • Tests recognition of star-graph topology and the insight that the central node is uniquely identified by its degree.

    Auto-gradedgraphsdegreestar-graph
  • Compute the smallest possible maximum edge weight along any path connecting two vertices in a weighted undirected graph.

    Auto-gradedgraphunion-findgreedybottleneck-path
  • Tests understanding of multi-source BFS to efficiently compute shortest distances from any of several starting points to many query locations.

    Auto-gradedbfsgraph-traversalshortest-pathgrid
  • Tests whether a candidate can exploit a single elimination insight to find a universal acquaintance in O(n) time instead of O(n²).

    Auto-gradedgraphseliminationtwo-pass
  • Tests weighted shortest-path reasoning under a hard constraint on the number of edges traversed, requiring a round-bounded relaxation strategy.

    Auto-gradedgraphsbellman-fordshortest-path
  • Tests depth-first traversal of a binary tree encoded as an indexed array to find paths matching a target sum.

    Auto-gradedbinary-treedfsiterativepath-sum
  • Tests knowledge of informed graph search, admissible heuristics, priority-queue mechanics, and canonical path reconstruction on a 2-D grid.

    Auto-gradedgraph-searcha-starbfsshortest-path
  • Determine whether any root-to-leaf path in a binary tree sums to a given target value.

    Model answerbinary-treedepth-first-searchtree-traversal
  • Tests directed graph modeling, cycle detection, and topological ordering using Kahn's algorithm or DFS.

    Auto-gradedtopological-sortgraphkahn-algorithmcycle-detection
  • Tests tree construction from parent pointers and post-order traversal to aggregate counts across nested organizational levels.

    Auto-gradedtreedfspost-orderrecursion
  • Find connected components in an asset correlation graph and return the summed position delta for each component as a labeled group.

    Model answergraph traversalconnected componentshash mapDFS
  • Tests graph reachability and cycle detection: determine whether a new directed edge can be added to an acyclic dependency graph without introducing a cycle.

    Auto-gradedgraphsdepth-first-searchcycle-detectionreachability
  • Find the minimum number of unit moves from the top-left to the bottom-right cell of a 0/1 matrix, or report that no route exists.

    Auto-gradedbfsgridshortest-pathgraph-traversal
  • Tests ability to apply topological ordering (Kahn's algorithm) to schedule parallel tasks with durations and prerequisite chains, computing the earliest completion time.

    Auto-gradedgraphstopological sortdynamic programmingBFS
  • Given a list of directed labeled edges, reconstruct the binary tree they describe and return its root node.

    Model answerbinary-treehash-maptree-construction
  • Tests grid flood-fill for connected-component counting and translation-invariant shape canonicalization to identify structurally distinct land regions.

    Auto-gradedgraph traversalflood fillconnected componentshashing
  • Tests constrained graph search: find the cheapest multi-leg flight route where only the first leg must depart within a given date range, connections must respect time ordering, and cycles are forbidden.

    Auto-gradeddijkstragraphsshortest-path
  • Tests graph reciprocity reasoning and set intersection: given directed follow edges, identify all users who are mutual friends with both members of every user pair.

    Auto-gradedgraphsset-intersectionsocial-network
  • Reason about degrees of freedom: since you may add arbitrary weight-1 edges, the optimal distance collapses to a constant.

    Auto-gradedgraphsshortest-pathreasoningedge-cases
  • Tests ability to combine flood-fill island identification with multi-source BFS to find the shortest water path bridging two disconnected land regions in a grid.

    Auto-gradedbfsgridflood-fill
  • Tests memoized path traversal in a parent-pointer hierarchy with cycle detection and ancestral filtering rules.

    Auto-gradedgraphsmemoizationcycle-detection
  • Build connected components from an edge list with Union-Find, then answer many connectivity queries quickly.

    Auto-gradedunion-findgraphdisjoint-setconnectivity
  • Tests multi-source BFS on a grid to find the open cell with the smallest total shortest-path distance to every station, respecting walls and blocked cells.

    Auto-gradedbfsgridshortest-path
  • Tests whether a candidate can find the longest edge-path in a tree using two linear-time traversals and reason about why the approach is correct.

    Auto-gradedbfstreesgraph-traversal
  • Find the length of the shortest 8-directional clean-cell path across a square 0/1 grid using BFS.

    Auto-gradedbfsgridshortest-path
  • Tests multi-source BFS on a 2D grid to find the minimum number of rounds for an infection to reach every susceptible cell, or detect if it cannot.

    Auto-gradedbfsmatrixmulti-source
  • Tests whether a candidate can model time-respecting graph search over indexed transit routes to determine if a destination is reachable from a starting stop.

    Auto-gradedgraph searchBFSgreedyshortest path
  • Tests shortest-path reasoning on a weighted directed graph, including source-caching strategy, zero-cost edges, unreachable nodes, and same-source queries.

    Auto-gradedgraphsshortest-pathdijkstra

More topics