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 31–60 of 434 results

  • Decode a level-order binary tree with null gaps, then return the k-th value in its inorder sequence, or -1 if fewer than k nodes exist.

    Auto-gradedbinary-treeinorder-traversaliterative-traversalstack
  • Answer permission queries over a DAG of groups where each group inherits from its parents, using graph traversal with memoization or caching.

    Auto-gradedgraphdagdfsmemoization
  • Choose a hub node and up to k adjacent nodes to maximize the total node value, which tests per-node greedy selection over adjacency lists.

    Auto-gradedgraphgreedysortingadjacency-list
  • Decide whether a goal node can be reached from node 0 in a directed graph without ever stepping on a forbidden node, using BFS or DFS reachability.

    Auto-gradedgraphbfsreachabilitydirected-graph
  • Replay unsorted timed link events and find the earliest timestamp at which every node is in one connected group, using union-find over events sorted by time.

    Auto-gradedunion-findgraph-connectivitysortingkruskal
  • Build a forest from (employee, manager) pairs and output each person with their depth in a sorted depth-first order, handling very deep chains iteratively.

    Auto-gradedtreedfsforesthash-map
  • Implement a single function that extracts leaves and a shape template from nested lists and dicts, then rebuilds the original from them, testing recursion and traversal order.

    Auto-gradedrecursiondfsnested-structuresdictionaries
  • Find the binary tree node value that appears across the highest number of unique depth levels.

    Model answertrees-graphsbreadth-first-searchhash-table
  • Compute, for every cell of a grid with walls and exits, the fewest steps to the nearest exit, marking walls and cut-off cells as -1, using multi-source BFS.

    Auto-gradedbfsgridmulti-source-bfsshortest-path
  • Compress a square character grid into a four-way region tree, then locate a batch of words in the grid by walking neighbors in eight directions without reusing cells.

    Model answerquadtreetriebacktrackingdivide-and-conquer
  • Find the minimum total cost between two nodes of a directed graph with non-negative edge weights, returning -1 if no route exists. Tests shortest-path reasoning with a priority queue.

    Auto-gradeddijkstrashortest-pathgraphheap
  • Sort an integer list without built-in sorting, then run a script of insert, search, delete and in-order commands on a duplicate-free binary search tree built from scratch.

    Auto-gradedbinary-search-treequicksortrecursiontree-deletion
  • Decide whether the vertices of an undirected multigraph can be split into two groups so every edge crosses between them, handling self-loops, repeated edges, and isolated nodes.

    Auto-gradedgraphbfsbipartiteconnected-components
  • Compute shortest-path distances from one source in an undirected graph with non-negative weights, handling parallel edges, self-loops, and unreachable nodes.

    Auto-gradeddijkstrashortest-pathgraphheap
  • For each node in a DAG whose links may name unknown items, count the distinct nodes reachable along link direction, counting shared descendants once.

    Auto-gradedgraphdagtopological-sortbitset
  • Decide whether a directed graph given as an adjacency list contains a cycle, handling self-loops, parallel edges, disconnected parts and an empty graph.

    Auto-gradedgraphdirected-graphcycle-detectiontopological-sort
  • A robot in an unseen grid senses neighbours for free and pays per move. Find a shortest route to a target, or return to the start and report failure if none exists.

    Model answerbfsgridonline-explorationshortest-path
  • Two linked tasks: list the nodes visible from the right of a binary tree, and delete the fewest parentheses from a string so the rest is balanced.

    Model answerbinary-treebfsstackstrings
  • Decide whether a weighted directed graph has a negative-total cycle reachable from a given source vertex, using Bellman-Ford while ignoring unreachable regions.

    Auto-gradedgraphsbellman-fordnegative-cycleshortest-path
  • Stream the leaf strings of a binary tree in left-to-right order and decide whether a pattern occurs in their concatenation, without ever building the full text.

    Auto-gradedbinary-treekmpstring-matchingiterative-dfs
  • Derive precedence constraints between letters from adjacent sorted words, then topologically sort them while detecting contradictions and invalid prefix orderings.

    Auto-gradedtopological-sortgraphstringscycle-detection
  • Compute the height of a binary tree given as a child-index table, handling empty input and very deep skewed trees without recursion failures.

    Auto-gradedbinary-treedfsiterative-traversaltree-height
  • Count connected groups of land cells on a grid whose opposite edges wrap around, testing grid traversal with modular adjacency.

    Auto-gradedgraphdfsgridunion-find
  • Two short coding tasks: suggest products that a user's friends bought but the user hasn't, and decide whether one URL can reach another through recorded navigation steps.

    Model answerhash-setset-differencegraphbfs
  • Produce a deterministic topological ordering of nodes from prerequisite pairs, or an empty list if a cycle makes it impossible. Tests graph modeling and Kahn's algorithm.

    Auto-gradedtopological-sortgraphbfscycle-detection
  • Evaluate a target variable from a list of assignment strings, growing from simple aliasing to signed sums of variables and integers, then detect circular dependencies safely.

    Model answerparsingmemoizationdfscycle-detection
  • Run a breadth-first traversal over a directed graph of folders, emitting each item once in first-seen order, while validating every reference and handling cycles.

    Auto-gradedbfsgraphhash-setdeduplication
  • Compute a deterministic level-ordered listing of every page reachable from a start page, where failed pages yield no links and concurrency limits must not change the result.

    Auto-gradedbfsgraphlevel-ordersets
  • Run a breadth-first search on a directed or undirected graph, skipping closed nodes, to get each node's minimum edge count from the start, or -1 if it cannot be reached.

    Auto-gradedbfsgraphshortest-pathreachability
  • Detect structural loops in both a next-pointer chain using constant space and a directed graph reachable from a given entry point.

    Auto-gradedtrees-graphslinked-listsdfstwo-pointers

More topics