KatInterview

Multi-Part Algorithm Challenge: Grids, Medians, and Traversals

Trees & GraphsAuto-graded · 19 test cases
MediumAuto-gradedTrees & Graphsgraphsarraysbinary-search

Last updated

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

You are given four inputs and must return a dictionary containing five results from three independent algorithmic tasks.

Task 1: Largest Island in a Binary Grid

Given an m × n grid containing only 0s and 1s, find the size of the largest island. An island is a maximal group of 1s connected through 4-directional adjacency (up, down, left, right). Diagonal connections do not count.

Return the count of cells in the largest island, or 0 if no island exists.

Task 2: Median Across Two Sorted Arrays

Given two sorted arrays a and b (each in non-decreasing order), compute the median of all elements combined as if they were merged into one sorted sequence.

  • If the total number of elements is odd, return the middle element as its original type (int remains int).
  • If the total number of elements is even, return the average of the two middle elements as a float.
  • If both arrays are empty, return None.

Task 3: Graph Traversals (BFS and DFS)

Given an undirected graph represented as an adjacency list graph where graph[i] is the list of neighbors of vertex i, return the visitation order for:

  1. Breadth-first search (BFS): Process vertices level by level.
  2. Depth-first search (DFS), recursive: Explore as deeply as possible before backtracking.
  3. Depth-first search (DFS), iterative: Use an explicit stack to replicate depth-first exploration.

The graph may be disconnected. Start from vertex 0, then 1, then 2, and so on, visiting each unvisited vertex in numerical order. Within each vertex, explore neighbors in the order they appear in the adjacency list.

For the iterative DFS, mark vertices as visited when they are pushed onto the stack (not when popped).

Output Format

Return a dictionary with these exact keys:

{
    'largest_component': int,
    'median': int | float | None,
    'bfs': List[int],
    'dfs_recursive': List[int],
    'dfs_iterative': List[int]
}

Constraints

  • 0 ≤ m, n ≤ 1000 (grid dimensions)
  • 0 ≤ len(a), len(b) ≤ 10^5
  • 0 ≤ len(graph) ≤ 1000 (number of vertices)
  • Each adjacency list may contain duplicates or self-loops
  • All array elements fit in a 32-bit signed integer

Examples

Example 1

Input: algorithm_suite([[1, 1, 0], [0, 1, 0], [1, 0, 1]], [1, 3], [2], [])

Output: {'largest_component': 3, 'median': 2, 'bfs': [], 'dfs_recursive': [], 'dfs_iterative': []}

Example 2

Input: algorithm_suite([[0, 0], [0, 0]], [1, 2], [3, 4], [[1], [0, 3], [3], [1, 2]])

Output: {'largest_component': 0, 'median': 2.5, 'bfs': [0, 1, 3, 2], 'dfs_recursive': [0, 1, 3, 2], 'dfs_iterative': [0, 1, 3, 2]}

Example 3

Input: algorithm_suite([[1, 1, 1], [1, 1, 1]], [1], [2], [])

Output: {'largest_component': 6, 'median': 1.5, 'bfs': [], 'dfs_recursive': [], 'dfs_iterative': []}

Hints

Hint 1

For the grid, use flood-fill from each unvisited cell containing a 1. Mark cells as visited immediately when discovered to avoid revisiting them.

Hint 2

For the median, avoid merging the arrays. Binary search over a partition point: choose how many elements to take from each array so the left half contains exactly ⌈(m+n)/2⌉ elements and every left element is ≤ every right element.

Hint 3

For graph traversals, use an outer loop over all vertices to handle disconnected components. In iterative DFS, push neighbors in reverse order so the first neighbor is processed first.