← Square Interview Insights

Square·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Square SWE interview that was basically one long graph problem broken into three escalating parts. The setup was clean but by part three I was definitely scrambling to keep the BFS logic straight while also talking through it out loud.

Questions Asked (3)

Q1

Design a class to record transactions between pairs of customers and answer whether two customers have ever been part of the same connected component in the transaction graph.

Algorithms & Data StructuresSystem DesignData Modeling
Author's notes

The union-find angle came to me pretty fast, which was good.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by modeling the problem as a graph where customers are nodes and transactions are edges, then use a Union-Find (Disjoint Set Union) data structure to efficiently track connected components. Design a class that supports adding transactions (union operations) and querying connectivity (find operations) with near-constant time complexity. Discuss trade-offs between Union-Find and other approaches like BFS/DFS, and consider optimizations like path compression and union by rank.

Pro tip: Mention that Union-Find is ideal for dynamic connectivity but if the graph is static, precomputing components with BFS/DFS might be simpler; also highlight that path compression and union by rank make operations practically O(1).

1. Clarify Requirements and Assumptions

Ask whether transactions are added incrementally or all at once, and whether queries are interleaved with updates. Clarify if customers are identified by IDs and if the graph is undirected (transactions imply mutual connection).

2. Choose Data Structure

Select Union-Find (Disjoint Set Union) for dynamic connectivity due to its efficiency. Explain that each set represents a connected component, and union merges sets when a transaction occurs.

3. Design Class Interface

Define methods: addTransaction(customer1, customer2) to union two customers, and areConnected(customer1, customer2) to check if they share the same root. Include a constructor to initialize parent and rank arrays.

4. Implement Union-Find with Optimizations

Use path compression in find to flatten the tree, and union by rank/size to keep trees shallow. This ensures nearly constant time per operation.

5. Analyze Complexity and Discuss Trade-offs

State that with optimizations, operations are O(α(n)) amortized, where α is the inverse Ackermann function. Compare with BFS/DFS which would be O(V+E) per query if graph is dynamic.

Key Points to Mention

  • Union-Find (Disjoint Set Union) data structure
  • Path compression and union by rank/size optimizations
  • Amortized time complexity O(α(n)) per operation
  • Handling dynamic graph updates efficiently
  • Alternative approaches: BFS/DFS for static graphs, but less efficient for dynamic connectivity
  • Edge cases: self-loops, duplicate transactions, non-existent customers

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

Q2

Extend your solution to return everyone reachable from a given customer through any chain of transactions, excluding the customer themselves.

Algorithms & Data StructuresSystem Design
Author's notes

Switched to BFS here and it felt like the right call.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the transaction network as a directed graph where customers are nodes and transactions are edges. Then perform a graph traversal (BFS or DFS) starting from the given customer, collecting all reachable nodes except the starting customer. Discuss handling cycles and large graphs efficiently.

Pro tip: Mention that you would use BFS with a visited set to avoid infinite loops in cyclic transaction networks, and consider distributed processing if the graph is too large for a single machine.

1. Clarify the problem

Confirm whether transactions are directed (e.g., payer to payee) and whether reachability should follow the direction of money flow. Ask about graph size and performance requirements.

2. Model as a graph

Represent customers as nodes and transactions as directed edges. If transactions are bidirectional, treat edges as undirected.

3. Choose traversal algorithm

Use BFS for shortest path or DFS for simplicity. Both work for reachability; BFS is often preferred for its iterative nature and ability to find shortest paths.

4. Implement traversal with visited set

Start from the given customer, mark as visited, and explore neighbors. Add each newly visited node to the result set, excluding the start node.

5. Analyze complexity and edge cases

Time complexity is O(V+E). Discuss handling cycles, disconnected components, and potential memory issues for large graphs.

Key Points to Mention

  • Graph representation: adjacency list for sparse graphs, adjacency matrix for dense graphs
  • BFS vs DFS trade-offs: BFS finds shortest paths, DFS uses less memory
  • Visited set to prevent infinite loops in cyclic graphs
  • Time and space complexity: O(V+E) time, O(V) space
  • Handling large-scale graphs: distributed graph processing (e.g., Pregel, GraphX) or streaming algorithms
  • Excluding the starting customer from the result set

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

Q3

Further extend the network query to accept a degree limit, returning only customers within at most N hops from the given person.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where things got messy for me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: the input is a graph of customers, a starting person, and a degree limit N. Then describe a BFS traversal that tracks depth and stops expanding beyond N, ensuring only nodes within N hops are returned. Discuss trade-offs like using BFS vs DFS, handling cycles, and potential optimizations for large graphs.

Pro tip: Mention that BFS naturally finds shortest paths in unweighted graphs, so it's ideal for degree-limited queries; also note that you can early-terminate when the queue's depth exceeds N to save work.

1. Clarify requirements and assumptions

Confirm that the graph is unweighted, edges represent connections, and the degree limit N is inclusive (i.e., up to N hops). Ask about graph size, whether it's directed or undirected, and if the starting person is included in the result.

2. Choose BFS for shortest-path guarantee

Explain that BFS explores nodes level by level, so the first time a node is visited, it's at its minimum hop distance from the start. This ensures we only include nodes within N hops.

3. Implement BFS with depth tracking

Use a queue storing (node, depth) pairs, a visited set to avoid cycles, and a result list. Enqueue the start with depth 0, then while the queue is not empty, dequeue, add to result if depth ≤ N, and enqueue unvisited neighbors with depth+1 only if depth < N.

4. Analyze complexity and trade-offs

State that time complexity is O(V + E) in the worst case, but with degree limit N it's O(b^N) where b is branching factor, which can be much smaller. Space is O(V) for visited and queue. Mention that DFS could work but may not find shortest paths and could explore deeper unnecessarily.

5. Discuss optimizations and edge cases

For large graphs, consider bidirectional BFS if N is small, or using a depth-limited search with iterative deepening. Handle edge cases: N=0 (only start), disconnected graph, cycles, and self-loops.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs, so it's the natural choice for degree-limited queries.
  • Track depth per node in the queue to enforce the N-hop limit and avoid exploring beyond it.
  • Use a visited set to prevent infinite loops in cyclic graphs and to avoid redundant processing.
  • Time complexity: O(V + E) without limit, but with limit N it's O(b^N) where b is average branching factor; space O(V).
  • Trade-off: DFS uses less memory but may not find shortest paths and could explore deeper than needed; BFS is safer for this requirement.
  • Edge cases: N=0 returns only the start; disconnected nodes are excluded; directed vs undirected affects traversal.

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