← Motive Interview Insights

Motive·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Motive software engineer interview that went deep on data structures, specifically Union-Find with all the trimmings. The graph connectivity piece at the end caught me a bit off guard since I expected to just implement the structure and move on.

Questions Asked (3)

Q1

Implement a Disjoint Set Union (Union-Find) data structure supporting make_set, find, union, and connected operations, with path compression and union by rank.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I've done Union-Find before but always kind of cargo-culted the path compression part without really internalizing why it works.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining the Disjoint Set Union (DSU) data structure with parent and rank arrays, then implement each operation with path compression and union by rank. Explain the optimizations and their impact on time complexity, and discuss trade-offs such as using rank vs. size and iterative vs. recursive find.

Pro tip: Mention that with both optimizations, the amortized time per operation is nearly constant (O(α(n))), and that path compression can be done iteratively to avoid stack overflow in large datasets.

1. Define the Data Structure

Explain that DSU maintains a forest of trees using a parent array and a rank (or size) array. Each set is represented by a root node.

2. Implement make_set

Initialize a new set with a single element: set its parent to itself and rank to 0 (or size to 1).

3. Implement find with Path Compression

Recursively or iteratively traverse to the root, then update each node's parent to point directly to the root to flatten the tree.

4. Implement union with Union by Rank

Find the roots of the two elements. Attach the tree with smaller rank under the root of the larger rank; if ranks are equal, increment the rank of the new root.

5. Implement connected and Analyze Complexity

connected(x, y) returns find(x) == find(y). Explain that with both optimizations, the amortized time per operation is O(α(n)), where α is the inverse Ackermann function.

Key Points to Mention

  • Path compression: makes find operations faster by flattening the tree structure.
  • Union by rank: keeps the tree height logarithmic by attaching smaller trees under larger ones.
  • Time complexity: nearly O(1) amortized per operation (O(α(n))).
  • Space complexity: O(n) for parent and rank arrays.
  • Trade-offs: union by size vs. rank; recursive vs. iterative find (stack overflow risk).
  • Use cases: Kruskal's algorithm, dynamic connectivity, image processing.

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

Q2

Given an undirected graph with n nodes and a list of edges, use your Union-Find implementation to answer multiple online connectivity queries efficiently. Walk through the approach and its complexity.

Algorithms & Data StructuresSystem Design
Author's notes

This felt like a natural extension but I tripped on the word 'online' for a second.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the Union-Find data structure with path compression and union by rank/size, then describe how to process the edges to build the disjoint sets. For each query, simply check if the two nodes have the same root. Conclude with the time complexity: O((n + m) α(n)) for building and O(α(n)) per query, where α is the inverse Ackermann function.

Pro tip: Mention that Union-Find is ideal for dynamic connectivity with only unions (no deletions), and highlight that the amortized near-constant time per operation makes it highly efficient for online queries. Also, briefly note that if the graph were dynamic with edge deletions, a different approach like Link-Cut Trees would be needed.

1. Explain Union-Find Basics

Describe the data structure: each set is represented by a tree, with the root as the representative. Explain the parent array and the find operation with path compression.

2. Describe Union by Rank/Size

Explain how to merge two sets by attaching the smaller tree under the larger one to keep the tree shallow, ensuring near-constant time operations.

3. Build the Structure from Edges

Iterate through the given edges and perform union operations for each edge. This builds the initial connected components.

4. Answer Queries

For each query (u, v), call find(u) and find(v). If the roots are the same, they are connected; otherwise, they are not.

5. Analyze Complexity

State that building takes O(m α(n)) time and each query takes O(α(n)) time, where α is the inverse Ackermann function, effectively constant. Space complexity is O(n).

Key Points to Mention

  • Path compression: flattens the tree during find operations, improving future queries.
  • Union by rank/size: keeps the tree height logarithmic, preventing degeneration.
  • Inverse Ackermann function α(n): grows extremely slowly, so operations are effectively O(1).
  • Amortized analysis: the near-constant time is amortized over a sequence of operations.
  • Online queries: Union-Find supports queries interleaved with unions, but not edge deletions.
  • Alternative for dynamic connectivity with deletions: Link-Cut Trees or Euler Tour Trees.

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

Q3

What edge cases would you handle in this Union-Find implementation, such as duplicate union calls or isolated nodes?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Isolated nodes I got immediately since make_set handles them.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the specific Union-Find implementation (e.g., with path compression and union by rank) and then systematically walk through edge cases, explaining how each is handled and why it matters. Emphasize both correctness and efficiency, and relate the edge cases to real-world scenarios like dynamic connectivity in large systems.

Pro tip: Mention that handling edge cases proactively prevents subtle bugs in production, and that Union-Find is often used in critical systems like network routing or image processing where robustness is key. Also, note that some edge cases (like duplicate unions) are naturally handled by the algorithm's design, which shows deeper understanding.

1. Clarify the implementation

Briefly state the assumed Union-Find variant (e.g., with path compression and union by rank) and its core operations (find, union, connected). This sets the context for edge cases.

2. Identify common edge cases

List typical edge cases: duplicate union calls, isolated nodes, self-unions, unions with invalid indices, and operations on empty sets. Explain what each means.

3. Explain handling and impact

For each edge case, describe how the implementation handles it (e.g., duplicate unions are no-ops if already connected) and the consequences if not handled (e.g., incorrect connectivity, performance degradation).

4. Discuss trade-offs and optimizations

Highlight how optimizations like path compression and union by rank mitigate edge cases and improve performance, and mention any trade-offs (e.g., extra space for rank array).

5. Conclude with testing and real-world relevance

Summarize the importance of testing these edge cases and relate them to practical applications (e.g., dynamic connectivity in networks, Kruskal's algorithm).

Key Points to Mention

  • Duplicate union calls: if elements are already in the same set, union should be a no-op to avoid cycles or unnecessary work.
  • Isolated nodes: each node starts as its own parent; find and union should work correctly without special handling.
  • Self-unions: union(x, x) should be a no-op; ensure it doesn't corrupt the structure.
  • Invalid indices: handle out-of-bounds or negative indices gracefully, possibly with exceptions or error codes.
  • Path compression and union by rank: these optimizations ensure near-constant time operations even with edge cases.
  • Testing: include unit tests for edge cases like empty sets, single node, and repeated unions.

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