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 91–120 of 434 results

  • Compute a conversion factor between two currencies by chaining and inverting listed rates, returning -1.0 when no chain exists; tests graph traversal with multiplicative weights.

    Auto-gradedgraphbfshash-mapweighted-edges
  • Find the non-adjacent node in an undirected graph with the maximum count of shared neighbors relative to a target node.

    Auto-gradedgraphsbfshash-tablesocial-network
  • Crawl a website graph from a start URL to an exit page using BFS, retrying transient 5xx failures a limited number of times, and return the minimum hop count or -1.

    Model answerbfsgraph-traversalretry-logicvisited-set
  • Aggregate subtree salary totals and headcounts bottom-up in a tree given as a flat list, then pick the best root using exact average comparison with size and id tie-breaks.

    Auto-gradedtreedfspost-ordertie-breaking
  • Find the cheapest route on a grid with free, paid and blocked cells plus same-numbered teleport cells, which calls for 0-1 BFS and care with teleport expansion.

    Auto-graded0-1 BFSshortest pathgridgraph modeling
  • Walk a nested structure of dicts and lists depth-first and gather every value stored under a given key, in traversal order, including matches inside already-matched values.

    Auto-gradedrecursiondfsjsontraversal
  • Construct a hierarchical forest from directed child-parent relations and output a depth-annotated preorder traversal.

    Auto-gradedtrees-graphsdepth-first-searchstackrecursion
  • Find the open cell with the smallest total shortest-path distance to a set of target cells on a grid with obstacles, using BFS and reachability counting.

    Auto-gradedbfsgridshortest-pathgraph-traversal
  • Compute the depth of a rooted tree given as a parent array after splicing out marked nodes, so their children attach to the nearest surviving ancestor.

    Auto-gradedtreebfsparent-arraydepth
  • Find the largest product of directed exchange rates along any path between two currencies, using Bellman-Ford-style relaxation on a graph that may contain cycles and rates above 1.

    Auto-gradedgraphbellman-fordshortest-pathcurrency
  • Determine whether empty grid plots can be assigned one of k colors without adjacent conflicts.

    Auto-gradedtrees-graphsarraysmath-bits
  • Simulate multi-source BFS propagation across a 2D grid up to a maximum number of steps.

    Auto-gradedbreadth-first-searchmatrixgraph-traversal
  • Decide whether a string becomes a palindrome after removing at most one character, then group a binary tree's node values by column using level-order tie-breaking.

    Model answertwo-pointerspalindromebfsbinary-tree
  • Compute the shortest total time to finish all jobs with unlimited parallelism by finding the longest weighted path in a dependency graph, returning -1 if a cycle exists.

    Auto-gradedtopological-sortdaglongest-pathdynamic-programming
  • Construct the lexicographically smallest topological ordering of nodes in a forest defined by an ancestor/parent mapping.

    Auto-gradedtrees-graphsheaps-sortinggreedy-intervals
  • Tests modeling word edits as an implicit graph and running BFS with wildcard buckets to find the minimum number of one-letter changes, with careful edge cases.

    Auto-gradedbfsgraphstringshashing
  • Compute how many pebbles can be taken off a board when each removal needs another remaining pebble in the same row or column, using connected components.

    Auto-gradedunion-findconnected-componentsgraph-modelingdfs
  • Combine contact records that are directly or transitively linked by a shared email, outputting each person's name with a deduplicated, sorted email list.

    Model answerunion-findgraphconnected-componentsdfs
  • Find the longest simple path from a start node in a directed graph with possible cycles, using backtracking DFS with a per-path visited set.

    Auto-gradedgraphdfsrecursionbacktracking
  • Solve five small tasks in one call: count graph components, minimum swaps to sort, largest monotonic number, simulate an LRU cache, and compare two N-ary trees.

    Auto-gradedgraph-traversalcycle-decompositiongreedy-digitslru-cache
  • Count 4-directionally connected clusters of 1s in a binary matrix using graph traversal, with care for visited tracking, empty inputs, and deep clusters.

    Auto-gradeddfsgridconnected-componentsmatrix
  • Brute-force enumerate looping command scripts of length 1 to 10 and simulate each on a grid with fuel, walls and refuel pads, using state-repeat detection to stop loops.

    Auto-gradedsimulationbrute-forcegridcycle-detection
  • Parse an indentation-based, pipe-delimited menu into a nested tree of dictionaries, testing stack-based hierarchy reconstruction and careful field parsing.

    Auto-gradedtree-constructionstackparsingstrings
  • Find the value in a Binary Search Tree represented as a level-order array that has the smallest absolute difference to a target number.

    Auto-gradedtrees-graphsbinary-search-treesearch
  • Compute the minimum number of 4-directional moves between two open cells of a binary maze, or -1 if unreachable; tests breadth-first search on an implicit grid graph.

    Auto-gradedbfsgridshortest-pathgraph
  • Given courses with durations, prerequisite links and a list of goal courses, compute the minimum time to finish the goals and their needed prerequisites, as total effort and as elapsed time.

    Model answergraphtopological-sortdagcycle-detection
  • Evaluates tree properties, cycle detection, edge validity, and in-order traversal under strict error precedence.

    Auto-gradedtreesgraphsdfstraversal
  • Turn a flat batch of scheduler tasks into a display list: merge duplicates, hide finished work, order by priority, and keep each subtask right under its parent.

    Model answersortinghashingtree-traversalcycle-detection
  • Design a reliable message passing protocol across adjacent tree nodes, then compute total network size and discover paths under network faults.

    Model answertrees-graphsdistributed-systemsmessage-passingnetwork-protocol
  • Turn a set of contest-style logic puzzles, led by the fox-chicken-grain river crossing, into formal models, and give algorithms with proofs of correctness and optimality.

    Model answerbfsstate-space-searchinvariantsproofs

More topics