← PayPal Interview Insights

PayPal·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

PayPal software engineer interview with a graph/union-find problem that looks deceptively simple until you realize brute-force swapping won't cut it at scale.

Questions Asked (1)

Q1

Given a string and a list of index pairs where you can swap characters any number of times, return the lexicographically smallest string possible.

Algorithms & Data Structures
Author's notes

My first instinct was to just simulate the swaps greedily and I wasted probably five minutes going down that path before realizing it doesn't work.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the index pairs as edges in an undirected graph where characters are nodes. Find connected components using union-find or DFS, then sort the characters within each component and place them in the smallest lexicographical order at the original positions.

Pro tip: Mention that the graph approach reduces the problem to sorting within components, achieving O(N log N) time, and discuss how to handle large inputs efficiently with union-find path compression.

1. Understand the problem

Clarify that swaps can be performed any number of times, meaning characters can be rearranged freely within connected components of the swap graph.

2. Build the graph

Create an undirected graph where each index is a node and each swap pair is an edge. Use union-find or adjacency lists to represent it.

3. Find connected components

Identify all connected components using DFS, BFS, or union-find. Each component represents a set of indices whose characters can be permuted arbitrarily.

4. Sort and assign characters

For each component, collect the characters at its indices, sort them, and assign the smallest characters to the smallest indices in the component.

5. Construct the result

Combine the assigned characters from all components to form the lexicographically smallest string.

Key Points to Mention

  • Graph representation of swaps as edges between indices
  • Connected components and their role in allowing arbitrary permutations
  • Union-find (disjoint set union) with path compression and union by rank for efficiency
  • Sorting characters within each component to achieve lexicographical minimality
  • Time complexity: O(N log N) due to sorting, space complexity: O(N)
  • Handling edge cases like empty string, no swaps, or all indices connected

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