Track Device Clusters as Shared Login Events Stream In

Last updated: November 29, 2026

Quick Overview

Integrity systems watch login events of the form (device_a, device_b) meaning the same account signed in on both devices. After each event, report the current number of distinct device clusters and the size of the largest one.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Integrity systems watch login events of the form (device_a, device_b) meaning the same account signed in on both devices. After each event, report the current number of distinct device clusters and the size of the largest one.

TikTok's trust and safety teams link devices to find fake account farms, and they need answers incrementally as events arrive rather than in a nightly batch. In technical screens this checks whether you can maintain union-find state online and update aggregates in O(1) per union.

What the Interviewer Expects
  • Chooses union-find because edges only get added, never removed
  • Adds devices lazily the first time they appear
  • Maintains the component count and max size incrementally
  • Uses path compression and union by size for near-constant time
  • Explains why BFS per event would be far too slow
Key Topics to Cover
union find
online algorithms
connected components
streaming
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 if links expire after 30 days, so edges get removed?
  • How would you flag a cluster the moment it crosses 50 devices?
  • How would you shard this across machines when the device graph does not fit in memory?
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: list of (a, b) device ID pairs, in arrival order.
  • Output: list of (num_clusters, largest_size) after each event.
  • A device not yet seen becomes its own cluster on first appearance.
  • `...
High-Level Design

Edges are only added, which is exactly the case union-find handles well. I keep a dict-based union-find so devices can appear at any time. Two aggregates: clusters increases by one when a new device...


Submit Your Answer
Markdown supported