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 211–240 of 434 results

  • Build an AST for variables, lambdas and applications, then infer each expression's most general function type using unification, with an occurs check to reject infinite types.

    Model answertype-inferenceunificationhindley-milnerast
  • Find the number of links on the path between two people in a company tree given as pairs, which tests undirected graph modeling and BFS or lowest common ancestor reasoning.

    Auto-gradedgraphbfstreeshortest-path
  • Each tree node is an isolated process with only async send and blocking receive. Design a protocol so the root learns the tree size, tolerates lost and duplicate messages, and returns a structured result.

    Model answerdistributed-systemsconvergecastidempotencyretries
  • Find every dictionary word that can be traced through orthogonally adjacent cells of a letter board without reusing a cell, using backtracking guided by a prefix tree.

    Auto-gradedbacktrackingtriedfsgrid
  • Compute, for every open cell of a grid with walls and exits, the shortest 4-directional distance to any exit using multi-source BFS, leaving unreachable cells unchanged.

    Auto-gradedbfsgridmulti-source-bfsmatrix
  • Filter a service inventory by namespace, attributes and status, then compute transitive dependencies and risk flags from a root service, handling cycles and dangling references.

    Auto-gradedgraph-traversaldfshash-mapfiltering
  • Run BFS on a character grid to return the shortest move sequence to any exit, breaking ties by a fixed U, D, L, R direction priority.

    Auto-gradedbfsgridshortest-pathpath-reconstruction
  • Find connected components in a signed grid and pick the best-scoring one, with and without a no-negatives rule, reporting its first and last row-major cells.

    Auto-gradedgriddfsconnected-componentstie-breaking
  • Sum durations over each root's subtree in a parent-linked record list and return the root with the largest total, testing tree aggregation and iterative traversal.

    Auto-gradedtreedfshash-mapiterative-traversal
  • Find the largest product of edge weights over simple directed paths between two nodes, using backtracking DFS, with a special case for identical endpoints and an unreachable case.

    Auto-gradedgraphdfsbacktrackingsimple-paths
  • Compute each node's final letter set in a DAG where grants flow to all descendants but revocations apply only to the node that declares them.

    Auto-gradedgraphdagtopological-sortbitmask
  • Answer many ratio queries between named quantities from pairwise ratio facts, using a weighted union-find or component precomputation instead of a search per query.

    Auto-gradedunion-findgraphweighted-union-findbatch-queries
  • Model one-way blast reach as a directed graph and find the largest set reachable from any single starting node, using exact integer distance checks.

    Auto-gradedgraphdfsreachabilitygeometry
  • Run a breadth-first search over (location, keys held) states in a forest with locked doors to find the first exit reached, or None.

    Auto-gradedbfsgraphstate-space-searchparsing
  • Project a heap-indexed binary tree onto a depth-by-column grid, then use BFS over non-zero cells to decide whether a cell holding the target is reachable from a given start.

    Auto-gradedbinary-treebfsgridvertical-order
  • Simulate a mini spreadsheet where formulas are evaluated at write time, dependents update eagerly, and any write that would create a dependency cycle is rejected without changing state.

    Auto-gradeddependency graphtopological sortcycle detectionsimulation
  • Build an independent deep copy of a nested container structure, preserving shared references and cycles, using a memo table and an explicit stack.

    Auto-gradeddeep-copygraph-traversalmemoizationiterative-dfs
  • Write a crawler that explores a maze of HTTP endpoints to find the exit, coping with cycles, transient 503 errors and malformed JSON, while guaranteeing termination.

    Model answergraph-traversalbfshttpretries
  • Apply a Minesweeper-style tap to a grid, expanding through zero-mine regions with breadth-first search and labeling cells with adjacent mine counts.

    Auto-gradedbfsgridmatrixsimulation
  • Compute, for every cell of a grid, the fewest orthogonal steps to the closest marked cell, using a single multi-source breadth-first search; -1 everywhere when no cell is marked.

    Auto-gradedbfsmulti-source-bfsgridshortest-path
  • Build a bidirectional weighted graph from exchange rates and answer conversion queries with a deterministic BFS that multiplies rates along the first-found path, or -1.0 if none exists.

    Auto-gradedgraphbfsweighted-graphhash-map
  • Backtracking over placements of named items on a grid under pairwise relative-position clues, deciding whether the target ends up in one cell across all valid layouts.

    Auto-gradedbacktrackingconstraint-satisfactiongridsearch
  • Combine topological ordering to find the minimum number of semesters (or detect a cycle) with a deque-based simulation of a two-rule request throttle that reports dropped timestamps.

    Auto-gradedtopological-sortgraphcycle-detectionsliding-window
  • Build a crawler that explores every page reachable from a seed URL using a slow, flaky link API, evolving from a sequential traversal to a thread pool with bounded, backoff-driven retries.

    Model answerweb-crawlerconcurrencybfsretries
  • Count 4-connected land regions in a grid, then report the updated region count after each cell is converted from water to land, using incremental connectivity.

    Auto-gradedunion-findgridconnected-componentsincremental-updates
  • Collect every page reachable from a seed URL on the same host, ignoring URL fragments and fetching each page once, first with one thread and then with safe concurrency.

    Model answerweb-crawlerbfsconcurrencythread-safety
  • Turn manager rows into a tree, infer the root, print it with depth-based dot indentation, or list every (manager, employee) pair exactly two levels apart.

    Model answertreepreorderiterative-dfshash-map
  • Compute the minimum cost to rewrite one string into another with directed priced letter swaps, using shortest paths over 26 letters and handling impossible positions and large totals.

    Auto-gradedgraphfloyd-warshallshortest-pathstrings
  • Run BFS on a link graph to find the lexicographically smallest fewest-hop route, where some pages fail a set number of times before their links load.

    Auto-gradedbfsgraphshortest-pathretries
  • Count ordered 4-vertex walks along edges in an undirected graph whose vertices carry one of four labels, where each walk uses every label exactly once.

    Auto-gradedgraphcountingcombinatoricsadjacency-list

More topics