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 241–270 of 434 results

  • Find the minimum time to complete all tasks with durations and prerequisites under unlimited parallelism, or detect a circular dependency, using longest path in a DAG with topological ordering.

    Auto-gradedtopological-sortgraphdaglongest-path
  • Count the directed edges of a tree that point toward a chosen root, which is the minimum number of flips needed to orient every edge away from it.

    Auto-gradedtreegraph-traversaldfsbfs
  • Compute when each grid cell becomes infected under a synchronous neighbor-count rule, using a multi-source BFS that updates counts incrementally instead of rescanning each round.

    Auto-gradedbfsgridsimulationmulti-source-bfs
  • Given a level-order serialized binary tree, return the first and last node value at each depth, testing BFS over an array-encoded tree and careful handling of null slots.

    Auto-gradedbinary-treebfslevel-orderserialization
  • Find the longest path, counted in edges, between any two nodes of a binary tree stored as a child-index list, testing tree height computation and avoiding deep recursion.

    Auto-gradedbinary-treedfsbfstree-height
  • Rebuild a binary search tree from its preorder listing of distinct integers in linear time, then report it in level order with null placeholders. Tests stack-based construction and BFS serialization.

    Auto-gradedbinary-search-treestackpreorderbfs
  • Implement a thread-safe crawler that explores links from a seed page to a given depth, never fetches a URL twice, and caps the number of simultaneous fetches.

    Model answerconcurrencybfsthread-poolgraph-traversal
  • Merge two keyed n-ary trees by summing values, encode them canonically, and strictly parse an encoded string with validation.

    Auto-gradedtreerecursionparsingserialization
  • Define a text format for ordered trees with arbitrary branching, then write an encoder and decoder that rebuild the exact same values, structure and child order in linear time.

    Model answertreeserializationn-ary-treeiterative-dfs
  • Produce the lexicographically smallest topological ordering of a dependency graph, or an empty list if the dependencies contain a cycle.

    Auto-gradedtopological-sortgraphheapcycle-detection
  • Transform a tree of Scan, Filter, Project and Join operators by moving filters toward the leaves and removing unused columns, while keeping query results unchanged.

    Model answertree-rewritingquery-optimizationpredicate-pushdownprojection-pruning
  • Compute the fewest +1 increments that make every root-to-leaf path total equal in a large rooted tree given as a parent array, which needs a bottom-up greedy and an iterative traversal.

    Auto-gradedtreegreedybfstree-dp
  • Build a valid course order from prerequisite pairs while skipping passed courses, using a topological sort with a fixed tie-break order and cycle detection.

    Auto-gradedtopological-sortgraphbfscycle-detection
  • Use recursion and backtracking to enumerate every ordered move sequence on a 4×4 board where three consecutive marks (not four) wins.

    Model answerbacktrackingrecursiongame-treecombinatorics
  • Tests in-order traversal of a binary search tree encoded in parallel arrays, with early termination and iterative stack management.

    Auto-gradedbinary-search-treein-order-traversaliterativestack
  • Tests grid-based connected-components detection using flood-fill or DFS, covering neighbor connectivity, visited-state tracking, and iterative traversal.

    Auto-gradedgraphsgriddfsconnected-components
  • Tests recursive AST traversal by simplifying an arithmetic expression tree through constant folding and algebraic identity rules, including cascading rewrites.

    Auto-gradedtreesrecursionast
  • Build a weighted graph from currency pairs and answer conversion queries, then extend to directional rates and arbitrage detection.

    Model answergraph-traversalshortest-pathbellman-fordfloating-point
  • Tests recursive-free N-ary tree traversal and set-based diffing to count nodes added, removed, or modified between two tree snapshots.

    Auto-gradedtreeshashingtraversal
  • Tests two skills at once: directional obstacle scanning on a 2D grid, and bitmask-based palindrome counting over rooted-tree ancestor segments.

    Auto-gradedgridbit-manipulationdfs
  • Find how many steps a contamination spreads from multiple sources through a grid, or determine if some cells remain permanently unaffected.

    Model answerBFSmulti-source BFSgrid traversalshortest path
  • Tests bottom-up tree traversal with greedy balancing, recognizing that shared ancestors minimize total increments across multiple paths.

    Auto-gradedbinary-treegreedypost-order-traversaltree-dp
  • Decide whether a word can be spelled by a non-repeating, four-directional path of adjacent cells in a character matrix, with pruning and complexity analysis.

    Model answerbacktrackingdepth-first-searchgridpruning
  • Tests ability to model a 2D grid as an unweighted graph and apply BFS to find the fewest moves between two corners while avoiding walls.

    Auto-gradedbfsgraphsgrid
  • Tests iterative graph traversal (DFS/BFS) on a binary grid, including bounds handling, visited-marking strategy, and complexity analysis.

    Auto-gradedgraph traversalDFSBFSmatrix
  • Tests whether a candidate can find the minimax-cost path in a filtered graph, recognising that one edge attribute gates inclusion while another drives the optimisation.

    Auto-gradedgraphdijkstraminimaxshortest-path
  • Design and code a React component that displays hierarchical data with interactive expand/collapse behavior and full keyboard accessibility.

    Model answerreactrecursionaccessibilitystate-management
  • Tests topological processing of a DAG where each node computes an active letter set from its parents' resolved sets plus its own additions and removals.

    Auto-gradedtopological-sortbit-manipulationgraph
  • Tests BFS-based grid traversal by counting distinct empty cells reachable from a starting position within a fixed number of 8-directional moves.

    Auto-gradedbfsgridgraph-traversal
  • Tests your ability to perform pre-order depth-first traversal on a tree structure and track hierarchical depth for each node.

    Auto-gradedtree-traversaldfsdepth-tracking

More topics