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 271–300 of 434 results

  • Transform a flat array of comments into a sorted, nested, filterable tree while persisting the user's sort preference across sessions.

    Model answertree-constructiondfslocalStoragefrontend-data
  • Tests whether a candidate can exploit BST ordering to locate the deepest node that is an ancestor of both targets without searching the entire tree.

    Auto-gradedbinary-search-treelowest-common-ancestortree-traversal
  • Tests understanding of binary tree traversal by computing boundary nodes at each depth using both breadth-first and depth-first strategies.

    Auto-gradedbinary-treebfsdfstree-traversal
  • Tests implementation of connected-component search in grids, efficient median computation across sorted sequences, and breadth-first and depth-first graph traversals.

    Auto-gradedgraphsarraysbinary-searchbfs
  • Tests building a tree structure from unordered edge list by tracking node relationships and identifying the root.

    Auto-gradedtreeshash-tablegraph-construction
  • Tests prefix-sum balancing for binary arrays and depth-first search for strictly increasing root-to-leaf paths in binary trees.

    Auto-gradedtreesarraysprefix-sumdfs
  • Recursively scan a directory tree, group files by size then content hash, and return every set of two or more paths that hold identical bytes.

    Model answerhashingfile-systemtraversaldeduplication
  • Given a tree as an adjacency list, return the sequence of nodes forming its diameter — the longest simple path between any two nodes.

    Model answerBFSDFStree diameterpath reconstruction
  • Tests whether you can model exchange rates as a weighted directed graph and apply Bellman-Ford to detect a cycle whose rate product exceeds 1.

    Auto-gradedgraphsbellman-fordlogarithms
  • Tests BFS on an undirected graph to filter nodes by shortest-path distance and a secondary inventory condition, including edge-case handling for missing keys and duplicates.

    Auto-gradedbfsgraphsshortest-path
  • Tests iterative functional-graph traversal: propagating True/False through chains of references and NOT, while correctly marking cycles, dangling references, and their dependents as unresolvable.

    Auto-gradedgraph traversalcycle detectionhash map
  • Traverse nested dictionaries and lists to collect dot-and-bracket paths to all string values, returned in sorted order.

    Auto-gradedtree-traversaliterationstackpath-building
  • Parse a LeetCode-style level-order array and return values grouped into nested lists by depth, preserving left-to-right order.

    Auto-gradedtreesbfsarrays
  • Tests tree traversal, ancestor queries, and handling multiple input formats with a custom precedence rule.

    Auto-gradedtreeslowest-common-ancestorgraph-traversal
  • Tests multi-source BFS on a grid to propagate shortest walking distances from all exits simultaneously to every reachable empty room.

    Auto-gradedbfsgraphsgrid
  • Tests whether a candidate can traverse an unordered weighted tree in linear time to extract the root, the heaviest root-to-leaf path weight, and the maximum depth.

    Auto-gradedtrees-graphshash-mapiterative-dfs
  • Given an undirected tree and multiple permutations of its nodes, determine for each permutation whether it could be the dequeue order of some BFS starting at its first element.

    Model answerBFStreessimulationhashing
  • Traverse a binary tree stored in heap-index layout to find every node whose subtree is height-balanced, returning each with its tree level.

    Model answerbinary-treepost-orderheap-layoutbottom-up
  • Tests binary search on a monotone feasibility predicate combined with multi-source BFS and Chebyshev distance geometry on a 2-D grid.

    Auto-gradedbfsbinary-searchgrid
  • Tests recursive spatial subdivision (quadtree construction) and graph cycle detection (identifying nodes that always terminate).

    Auto-gradedrecursiongraphsdivide-and-conquertopological-sort
  • Tests ability to traverse nested data structures and transform hierarchical key-value pairs into a flat representation using path notation.

    Auto-gradedrecursioniterationdictionarytree-traversal
  • Given a grid with depot cells, find shortest paths from query locations to any depot, then extend to pick the depot maximizing a distance-penalized load score.

    Model answerBFSDijkstramulti-sourcegrid
  • Bucket level-order tree nodes by column, then sort each column by depth and value to produce the grouped output.

    Auto-gradedbinary-treebreadth-first-searchsortinghash-map
  • Tests ability to find connected components in an undirected graph and compute the maximum difference between the highest and lowest node labels in any single component.

    Auto-gradedunion-findgraphsconnected-components
  • Tests BFS-based shortest-path traversal on an undirected graph, including path reconstruction and degree-of-separation counting with correct edge-case handling.

    Auto-gradedgraphsBFSshortest-path
  • Given a DAG where each node carries allow and deny lists, compute every node's effective permissions by inheriting and merging rules along edges in topological order.

    Model answergraphstopological-sortpermissionsDAG
  • Given preorder and inorder traversal arrays of a binary tree with unique values, reconstruct the tree in O(n) time and O(n) space.

    Model answerbinary-treerecursionhash-maptraversal
  • Build a validated org-chart tree from manager–report pairs and find the deepest node that is an ancestor of every node in a given set.

    Model answerlowest-common-ancestorbinary-liftingtree-traversalinput-validation
  • Given a list of words, merge them into one string by joining where each word's last letter matches the next word's first letter, dropping the duplicate.

    Model answereulerian-pathgraphstringshierholzer
  • Tests graph traversal skills by counting connected components in a character grid where land cells are neighbours in all eight directions including diagonals.

    Auto-gradedgraph traversalBFS/DFSflood fillconnected components

More topics