Count Isolated Store Clusters in a Regional Supply Network

Last updated: November 29, 2026

Quick Overview

Stores are numbered 0 to n-1 and a list of two-way truck lanes connects pairs of stores. Return how many connected clusters exist and the size of the largest one.

Walmart Labs
Coding & Algorithms
Software Engineer
Walmart Labs
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Stores are numbered 0 to n-1 and a list of two-way truck lanes connects pairs of stores. Return how many connected clusters exist and the size of the largest one.

An online assessment style graph question tied to Walmart's regional distribution network, where clusters of stores that can share inventory need to be identified. The grader checks a correct traversal with a visited array and no recursion depth problems on large inputs.

What the Interviewer Expects
  • Build an adjacency list
  • Traverse each unvisited store with DFS or BFS
  • Count clusters and track the largest size
  • Avoid recursion limits on big graphs
  • Handle self loops and duplicate lanes
Key Topics to Cover
graphs
DFS
connected components
adjacency list
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you answer the same question if lanes arrive one at a time?
  • How would you find the lane whose removal splits a cluster?
  • What if lanes are one-way, so you need strongly connected clusters?
  • How would you scale this to millions of stores and suppliers?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: integer n and a list of (a, b) pairs. Lanes are undirected, may repeat, and may be self loops. Output: a tuple (clusters, largest). A store with no lanes is its own cluster of size 1, and n...

High-Level Design

Build an adjacency list, then sweep through stores. Each time an unvisited store appears, start a new traversal, which visits exactly that store's cluster. An explicit stack avoids Python recursion li...


Submit Your Answer
Markdown supported