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 151–180 of 434 results

  • Answer batched queries on a static directed follow graph: list direct followees and rank second-hop accounts by how many followees also follow them.

    Auto-gradedgraphhash-mapadjacency-listsorting
  • Group binary tree nodes by horizontal column, ordering each column by depth and then by value, with the tree given as a level-order list with nulls.

    Auto-gradedbinary-treebfssortingcoordinates
  • Find the minimum number of monster cells entered on any route from start to exit in a grid with walls, using a weighted shortest-path search such as Dijkstra or 0-1 BFS.

    Auto-gradedgriddijkstrashortest-path0-1-bfs
  • Choose one shift per element from a given list to maximize distinct results; tests recognizing the problem as bipartite matching and implementing Hopcroft-Karp efficiently.

    Auto-gradedbipartite-matchinghopcroft-karpgraphsgreedy-pitfalls
  • Traverse a nested directory dictionary depth-first and build the full slash-joined path for each file, following precise joining and ordering rules.

    Auto-gradeddfstree-traversalrecursionstrings
  • Design and implement a React component to display nested JSON with collapsible nodes, indentation, and syntax highlighting for primitive types.

    Model answerReactJSONTreeFrontend
  • Group nodes of a binary tree represented in array heap layout into vertical columns ordered from left to right.

    Auto-gradedtrees-graphsbreadth-first-searchbinary-treequeues
  • Evaluate grid graph connectivity by finding the number of distinct 4-directionally connected land components.

    Auto-gradedtrees-graphsdfs-bfsgrid
  • Compute the earliest time each cell of a square grid is reached by a wave spreading from several seed cells, using multi-source breadth-first search.

    Auto-gradedbfsgridmulti-sourceshortest-path
  • Check if a list of integers forms a complete valid root-to-leaf path in a binary tree given as an adjacency table.

    Auto-gradedtrees-graphstreearray
  • Group event records by user, order each user's events by time, merge the sequences into a prefix tree with distinct-user counts, and print it in a fixed preorder format.

    Auto-gradedtriehash-mapstable-sortdfs
  • Process referral relationships with at most one parent per user. Detect cycles, extract acyclic prefixes, and compute root ancestors and depths for all users in O(n) total time.

    Model answertrees-graphsdfscycle-detectiongraph-traversal
  • Perform a flood-fill recoloring on a 2D integer grid while avoiding call stack overflow and preserving the original input.

    Auto-gradedbfs-dfsmatrixflood-fillgraph-traversal
  • Evaluate symbolic variable definitions with standard arithmetic precedence, resolving variable dependencies while detecting cycles and missing definitions.

    Auto-gradedtrees-graphsstacks-queuestopological-sortrecursion
  • Find the largest connected cluster in a list of two-digit numbers where links come from shared digits, testing graph connectivity modeling with union-find or digit-hub nodes.

    Auto-gradedunion-findconnected-componentsbitmaskgraph
  • Find the minimum travel distance from every destination point to its closest source hub on a 2D grid using multi-source Breadth-First Search.

    Auto-gradedBFSMulti-Source BFSGrid TraversalShortest Path
  • Evaluate a split-based arithmetic expression, returning false the moment any literal or intermediate value is non-positive or non-integer.

    Model answerrecursionexpression-evaluationpost-orderparsing
  • Tests breadth-first search on a square binary grid with eight-directional moves, handling blocked endpoints, visited tracking, and counting cells along the shortest route.

    Auto-gradedbfsgridshortest-pathgraph-traversal
  • Combine a merge-style sparse vector dot product with an iterative flood fill that counts 4-connected land clusters, returning both results together.

    Auto-gradedtwo-pointerssparse-vectorgriddfs
  • Find the shortest distance between two web pages in a directed link graph using breadth-first search.

    Auto-gradedbfsgraphshortest-pathtrees-graphs
  • Find all orthogonal connected components of non-zero cells in a 2D grid and calculate the total sum of cell values for each component.

    Auto-gradedtrees-graphsbreadth-first-searchdepth-first-searchmatrix
  • Rebuild a BST from a level-order list and sum the node values inside an inclusive range, using ordering to skip subtrees that cannot contain qualifying values.

    Auto-gradedbinary-search-treedfstree-constructionpruning
  • Build a pair of functions that flatten a binary tree of integers into a string and restore the identical tree from that string, with a clear format and a correctness argument.

    Model answerbinary-treeserializationpreorderstack
  • Run one BFS from the top-left cell of a grid with distinct values, using strictly increasing moves, then answer many target queries by rebuilding paths from stored parent links.

    Auto-gradedbfsgridshortest-pathpath-reconstruction
  • Find all comments hidden from a reader when any flagged comment hides its whole reply subtree, handling unordered input, dangling parent IDs, and very deep chains.

    Auto-gradedtreedfshash-mapforest
  • Produce a full run order for jobs under prerequisite constraints, always picking the alphabetically smallest available job, or return an empty list if a cycle exists.

    Auto-gradedtopological-sortheapgraphkahn-algorithm
  • Organize the elements of a binary tree into vertical columns using level-order traversal rules on a level-order array input.

    Auto-gradedtrees-graphsbreadth-first-searcharraysqueues
  • Find the k-th node reached during a preorder depth-first traversal of a specific node's subtree when children are visited in ascending numerical order.

    Auto-gradedtrees-graphsdfstree-traversal
  • Find the lowest common ancestor of two nodes in a binary tree, then generalize to a directed acyclic graph where several incomparable lowest common ancestors can exist.

    Model answerlowest-common-ancestorbinary-treedagbfs
  • Determine the final visited node when following single-successor directed edges until reaching a dead end or a previously visited node.

    Auto-gradedtrees-graphsarrayshash-table

More topics