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 61–90 of 434 results

  • Simulate one Minesweeper tap on a grid, flood-revealing connected zero-count cells with an iterative traversal and labelling border cells with their 8-neighbour mine counts.

    Auto-gradedbfsflood-fillgridmatrix
  • Find the largest edge count of a triangle-free simple graph on n vertices and output a canonical extremal graph, testing Mantel's theorem and the complete bipartite construction.

    Auto-gradedgraph-theoryconstructionbipartitemantel-theorem
  • Compare two keyed N-ary trees given as nested dicts and count nodes that were added, removed, given a new value, or attached to a different parent.

    Auto-gradedtreehash-mapdfstree-diff
  • Decide whether tasks with prerequisite constraints can all be run, and if so produce the lexicographically smallest valid run order, using topological sorting with a min-heap.

    Auto-gradedtopological-sortgraphheapcycle-detection
  • Build an undirected graph from friendship pairs, then rank every non-friend of a target user by mutual friends, degree and ID using a composite sort key.

    Auto-gradedgraphadjacency-listsortingsets
  • Produce a deterministic course order that respects prerequisite pairs using a queue-based topological sort, or an empty list if the prerequisites contain a cycle.

    Auto-gradedtopological-sortgraphbfscycle-detection
  • Find the deepest node that is an ancestor of two given values in a binary tree stored as a heap-indexed array, using a parent map and ancestor walk.

    Auto-gradedbinary-treelowest-common-ancestorbfshash-map
  • Determine a valid sequential execution order for a set of tasks given dependency rules, or detect impossible circular dependencies.

    Auto-gradedtopological sortgraphbfsdegree count
  • Solve two fundamental interview tasks: identifying the rightmost nodes of a binary tree by level and finding non-duplicate zero-sum triplets in an un-ordered array.

    Model answerbinary-treebfstwo-pointerssorting
  • Count, for each node of a weighted tree, the unordered pairs of other nodes at distances divisible by k and lying in different branches around that node.

    Auto-gradedtreedfscountingcombinatorics
  • Count groups of touching 1-cells in a binary grid where all eight surrounding cells count as neighbors, testing grid traversal and connected-component counting.

    Auto-gradedgridbfsconnected-componentsmatrix
  • Compute distances in a tree from three marked vertices and count the other vertices whose three distances form a right-triangle (Pythagorean) triple.

    Auto-gradedbfstreegraph-traversalmath
  • Guide a robot through a map it can only discover step by step, reaching the target with no repeated visits, real backtracking, and a clear result when the target is unreachable.

    Model answerdfsbacktrackinggraph-traversalgrid
  • Find the one node in a directed relation that everyone points to and that points to nobody, using a linear elimination pass followed by verification.

    Auto-gradedgraphcelebrity-problemtwo-passelimination
  • Find the lexicographically smallest valid ordering of nodes under child-before-parent and extra precedence rules, or report impossibility when the rules contradict.

    Auto-gradedtopological-sortheapcycle-detectiongraph
  • Compute the minimum of every K x K window in a grid, then order recipes so that every derived ingredient is made before the dish that uses it, detecting cycles.

    Model answertopological-sortmonotonic-dequesliding-windowkahns-algorithm
  • Produce a deterministic topological ordering of courses from prerequisite pairs, or an empty list when a circular requirement makes it impossible.

    Auto-gradedtopological-sortgraphbfscycle-detection
  • Given directed currency exchange quotes, decide whether some round trip returns more than it started with, and output one such round trip as an ordered list of currencies.

    Model answergraphbellman-fordnegative-cyclefloating-point
  • Find the smallest start index in a jump array whose walk never goes below index 0, handling cycles and right exits, ideally in linear time.

    Auto-gradedfunctional-graphcycle-detectionmemoizationarrays
  • Find the unique path between two pre-order-indexed nodes in a huge recursively defined tree, using subtree sizes to navigate without building it.

    Auto-gradedtreelcarecursionpre-order
  • Find the route between two nodes in an undirected graph that maximizes the product of per-link delivery probabilities, using a Dijkstra-style search with a max-priority queue.

    Auto-gradedgraphdijkstraheapshortest-path
  • Build a height-balanced BST from a sorted list using a fixed middle-element rule, then serialize it level by level with null placeholders.

    Auto-gradedbinary-search-treedivide-and-conquerrecursionlevel-order-traversal
  • Find the fewest moves through a grid where lowercase cells give keys and uppercase cells are gates, using BFS over position plus the set of collected keys.

    Auto-gradedbfsgridstate-space-searchkeys-and-doors
  • Reassemble an unordered collection of trace span objects into a forest of child-linked execution trees sorted chronologically.

    Auto-gradedtreeshash-tablesortinggraph
  • Construct an adjacency graph from reporting links and perform a level-order traversal to group node IDs by depth.

    Auto-gradedtrees-graphsbfshash-map
  • Find the largest connected land region achievable by turning at most one water cell into land, which tests component labeling and merging neighbors without double counting.

    Auto-gradedgridflood-fillconnected-componentsdfs
  • Decide whether a set of people can be placed into two teams so that no rival pair shares a team, which tests graph 2-coloring across possibly disconnected components.

    Auto-gradedgraphbipartitebfscoloring
  • Traverse a nested dictionary tree in pre-order, tracking depth to build indented lines with a fixed marker, preserving the given child order.

    Auto-gradedtree-traversaldfsstackstrings
  • Decide whether a set of ordering rules between numbered modules contains a circular dependency, by detecting a cycle in a directed graph.

    Auto-gradedgraphtopological-sortcycle-detectionbfs
  • Determine the shortest path distance between two nodes in an unweighted, undirected graph using Breadth-First Search.

    Auto-gradedbfsgraphshortest-pathhash-map

More topics