← PayPal Interview Insights

PayPal·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026Remote

Summary

PayPal coding screen, pretty much just one graph problem. Nothing fancy, no behavioral, just get the answer and move on.

Questions Asked (1)

Q1

Given an n x n adjacency matrix representing direct connections between cities, return the total number of connected components (provinces) in the graph.

Algorithms & Data Structures
Author's notes

Classic union-find or DFS problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the cities as nodes in an undirected graph and count the number of connected components. Use either DFS/BFS to traverse each component or Union-Find to merge connected cities, then count the distinct sets.

Pro tip: Clarify whether the graph is undirected (adjacency matrix is symmetric) and mention that you can optimize space by using a 1D visited array instead of a full matrix copy. Also, briefly discuss trade-offs between DFS and Union-Find for large n.

1. Understand the problem

Confirm that the adjacency matrix represents an undirected graph where matrix[i][j] = 1 means city i and city j are directly connected. The goal is to count the number of connected components (provinces).

2. Choose an algorithm

Decide between DFS/BFS and Union-Find. DFS/BFS is straightforward for counting components; Union-Find is efficient for dynamic connectivity but overkill here. Mention both and pick one based on constraints.

3. Implement traversal or union

For DFS/BFS: iterate over all cities, and for each unvisited city, increment the component count and traverse all reachable cities, marking them visited. For Union-Find: initialize each city as its own parent, union connected cities, then count distinct roots.

4. Analyze complexity

State time and space complexity: DFS/BFS is O(n^2) time and O(n) space; Union-Find with path compression and union by rank is O(n^2 α(n)) time and O(n) space.

5. Test with examples

Walk through a small example (e.g., n=3 with matrix [[1,1,0],[1,1,0],[0,0,1]]) to verify the count is 2. Discuss edge cases like n=1 or fully connected graph.

Key Points to Mention

  • Graph representation: adjacency matrix as an undirected graph
  • Connected components definition and counting
  • DFS/BFS traversal with visited array
  • Union-Find (Disjoint Set Union) with path compression and union by rank
  • Time and space complexity analysis
  • Edge cases: n=1, no edges, fully connected

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.