Frontend School

Swiggy Delivery Zone Unification

DSA
easy

Swiggy Delivery Zone Unification

Easy
Union Find
Graph
Data Structures

Overview

Swiggy operates a vast network of delivery partners and restaurants. To optimize logistics and understand service coverage, it's crucial to identify distinct "delivery zones" – areas where any two locations can be reached from each other through a series of connected points. This problem asks you to help Swiggy by determining how many such independent delivery zones exist given a set of direct connections between various zones.

What you need to do / Task

Implement a function countDeliveryZones that takes the total number of delivery zones (n) and a list of direct connections between them. Your task is to return the total number of distinct, unconnected delivery zones that exist based on these connections.

Input & output

  • n: An integer representing the total number of delivery zones, labeled from 0 to n-1.
  • connections: An array of arrays, where each inner array [zoneA, zoneB] indicates a direct, bidirectional connection between zoneA and zoneB.
  • Return: An integer, the total count of distinct delivery zones.

Clarifications

  • Zones are 0-indexed.
  • A connection [u, v] implies u and v are directly connected, and this connection is bidirectional.
  • If no connections are provided, each zone is considered a distinct delivery zone.
  • A zone is always considered connected to itself.

Examples

1

Example 1

Input:

n = 4, connections = [[0, 1], [2, 3]]

Output:

2

Note:

Zones {0, 1} form one delivery zone, and zones {2, 3} form another. These two zones are distinct.

2

Example 2

Input:

n = 3, connections = [[0, 1], [1, 2]]

Output:

1

Note:

Zones {0, 1, 2} are all connected, forming a single delivery zone.

3

Example 3

Input:

n = 5, connections = []

Output:

5

Note:

Without any connections, each of the 5 zones remains distinct.

Constraints

Constraints & assumptions

  • 1 <= n <= 1000
  • 0 <= connections.length <= n * (n - 1) / 2
  • 0 <= zoneA, zoneB < n
  • zoneA != zoneB (connections do not include self-loops)
  • Each connection [u, v] appears at most once (no duplicate connections).

Out of scope

  • Calculating shortest paths or optimal routes within a zone.
  • Handling real-world geographical coordinates or distances.
  • Dynamic updates to connections (all connections are given upfront).

What interviewers evaluate

  • Correctness of the Union-Find algorithm implementation (find and union operations).
  • Ability to correctly count the number of disjoint sets (delivery zones).
  • Efficiency of the solution in terms of time and space complexity.
  • Clarity, readability, and maintainability of the code.

Loading editor...