Answer Same-Organization Queries While Business Account Merges Stream In

Last updated: November 29, 2026

Quick Overview

You process a stream of operations on n business accounts: ('merge', a, b) combines the organizations containing a and b, and ('same', a, b) asks whether a and b currently belong to the same organization. Return the answers to every 'same' query in order.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


You process a stream of operations on n business accounts: ('merge', a, b) combines the organizations containing a and b, and ('same', a, b) asks whether a and b currently belong to the same organization. Return the answers to every 'same' query in order.

A Meta phone screen style question that introduces union find through Business Manager, where ad accounts and pages get consolidated under one business over time. The interviewer is checking a clean disjoint set implementation with path compression and union by size, and that you can explain the near-constant amortized cost.

What the Interviewer Expects
  • Implement find with path compression
  • Implement union by size or rank
  • Answer each query in near O(1) amortized time
  • Explain why BFS per query is too slow for a long stream
  • Handle merging two accounts already in the same org
Key Topics to Cover
union find
path compression
union by size
online connectivity
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What is the inverse Ackermann function and why does it appear here?
  • How would you support splitting an organization apart?
  • How would you shard this structure if there are billions of accounts?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: n accounts 0..n-1 and ops, a list of tuples ('merge', a, b) or ('same', a, b).
  • Output: list of booleans for the 'same' operations.
  • The stream can be long (millions of ops), so...
High-Level Design

Merges only ever combine groups and never split them, which is exactly the setting for a disjoint set union.

  • parent[x] points toward the representative of x's group.
  • find(x) follows parents...

Submit Your Answer
Markdown supported