Quick Overview

This question evaluates a candidate's competence in designing efficient data structures and streaming/top‑K algorithms, handling event-driven updates, correctness under late or expired events, deterministic tie‑breaking, and scalability to millions of customers.

Compute top-N active customers

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: HR Screen

Maintain and return the top N customers by cumulative activity amount, where activity is defined as the sum of deposit amounts, pay amounts, and transfer amounts only when the transfer is successfully accepted (expired transfers contribute nothing). Design data structures and an API (e.g., getTopNActivity(N)) that support near‑real‑time updates as operations occur and efficient queries. Specify update and query complexities, handle late accept events that change a customer’s activity, ensure correctness when transfers expire, define deterministic tie‑breaking (e.g., by customerId then most‑recent activity), and discuss how you would handle changing N between queries and scale to millions of customers.

Quick Answer: This question evaluates a candidate's competence in designing efficient data structures and streaming/top‑K algorithms, handling event-driven updates, correctness under late or expired events, deterministic tie‑breaking, and scalability to millions of customers.

Simulate customer activity updates and top-N queries with deterministic tie-breaking by customer id.

Constraints

  • deposit and pay add amount to that customer.
  • An accepted transfer adds amount to both endpoints; expired transfers do not count.

Examples

Input: ([["deposit","c1",10],["pay","c2",7],["transfer","t1","c1","c2",5],["top",2],["accept","t1"],["top",2]],)

Expected Output: [[['c1', 10], ['c2', 7]], [['c1', 15], ['c2', 12]]]

Explanation: Accepted transfers add activity to both endpoints.

Input: ([["transfer","t1","a","b",3],["expire","t1"],["accept","t1"],["top",2]],)

Expected Output: [[]]

Explanation: Expired transfers contribute nothing.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.

Loading coding console...