If time difference is greater than heartbeat, user should be offline, not online.
Step 2: Correct the comparison
The condition should return 'offline' when difference > heartbeat, so the comparison sign must be reversed.
Final Answer:
The comparison sign should be reversed -> Option B
Quick Check:
Time diff > heartbeat means offline [OK]
Hint: Online if time diff ≤ heartbeat, else offline [OK]
Common Mistakes:
Returning online when user is inactive
Misunderstanding heartbeat meaning
Ignoring return type correctness
5. You need to design an online presence system for a messaging app with millions of users. Which approach best ensures scalability and real-time accuracy?
hard
A. Store presence data in user profile tables updated once per day
B. Store user status in a centralized database and query it on every client request
C. Send presence updates only when users log in or log out, ignoring heartbeats
D. Use distributed in-memory cache with TTL and heartbeat updates from clients
Solution
Step 1: Evaluate centralized database approach
Querying a centralized DB for every request causes high latency and bottlenecks at scale.
Step 2: Consider distributed cache with TTL and heartbeats
This approach keeps presence data fresh, reduces DB load, and supports real-time updates efficiently.
Step 3: Analyze other options
Ignoring heartbeats or updating once per day leads to stale presence info, unsuitable for real-time apps.
Final Answer:
Use distributed in-memory cache with TTL and heartbeat updates from clients -> Option D