AlgoViz

Design a LinkedIn-style connection graph

hard 55 min

Store who is connected to whom for a billion people, and say in a blink whether a stranger is a 1st, 2nd or 3rd degree connection.

See it run

Connect two people, measure the distance between them, and suggest the next connection, one hop at a time.

Operations

Send and accept an invitation

The database decides, and everything else follows from its log.

Step 1 of 11

Asha presses Connect

Our systemAshaAPI gatewayConnection svcPostgreSQLtruth + outboxKafkachange logGraph shardsfriend listsNotificationsLee

target

Lee

The app sends the target, an optional message and an idempotency key. Why: a retry on a bad connection must not create a second invitation.

Step 1: Asha presses Connect

def invite(sender, target, key):    limiter.check(sender)                      # e.g. 100 a week    low, high = sorted((sender, target))    if blocks.between(low, high): return pending_lookalike()    row = db.insert_if_no_open(low, high, sender)   # partial unique index    if row.sender != sender: return accept(row)     # both asked    return rowdef accept(row):    with db.transaction():        row.status = "accepted"        db.insert_connection(row.low, row.high)        db.outbox.add("connected", row.low, row.high)
Latency
< 300 ms
Load
580 accepts/s
Peak (3x)
1,740/s
Invitations
1,160/s

The connection and its outbox row are one transaction, so the graph can be late but never wrong for good.

In the real world

The same connection graph doing real jobs. Pick one and step through it.

The target has 30,000 connections.

Step 1 of 4

Two very different lists

Our systemViewerAPI gatewayDegree serviceGraph shardslists in memoryTwo-hop setsper viewerPrivacy rules
mine
500
theirs
30,000

500 against 30,000.

Step 1: Two very different lists

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Walking outward

    critical

    Two hops read 250,000 entries and three hops up to 125 million.

    Wrong
    for hop in range(3):    frontier = all_friends(frontier)
    Right
    if b in friends(a): return 1if overlap(friends(a), friends(b)): return 2
  • Sampling a supernode

    critical

    A missing mutual friend turns a 2nd degree into a 3rd. Use the short list instead.

    Wrong
    sample = random.sample(big, 1000)return any(x in mine for x in sample)
    Right
    return any(x in big for x in small)
  • Guarding one direction only

    major

    Two people can invite each other at the same time and both rows are accepted by the constraint.

    Wrong
    UNIQUE (sender_id, receiver_id)
    Right
    CREATE UNIQUE INDEX one_openON invitations (low_id, high_id)WHERE status = 'pending';
  • Caching each pair

    major

    Pairs rarely repeat, and one accept would have to delete entries you cannot list.

    Wrong
    cache.set(f"deg:{a}:{b}", 2, ttl=900)
    Right
    cache.set(f"hop2:{viewer}", two_hop, ttl=600)
  • Writing two stores in one request

    critical

    If the second write fails, the copies differ for ever.

    Wrong
    db.insert(conn)graph.add(conn)
    Right
    with db.transaction():    db.insert(conn)    db.outbox.add(conn)

Interview tips

  • Lead with numbers

    • "All lists are 2 TB, so they fit in memory."
    • 69,000 profile checks a second at peak.
    • Three hops one way is 125 million people.
  • Explain trade-offs

    • Hash shards: even load, more fan-out.
    • Set per viewer: fast pages, ten minutes old.
    • Memory copy: speed for a second store.
  • Anticipate failures

    • Set cache lost: slower, never wrong.
    • Log paused: distances freeze, then catch up.
    • Privacy unreachable: hide.
  • Show care

    • Blocks hold on the next request.
    • Suggestions have allowed signals only.
    • A hide button that is remembered.