Compare Wrapping Packet Sequence Numbers for Game Sync

Last updated: November 29, 2026

Quick Overview

A real-time game protocol over UDP uses 16-bit sequence numbers that wrap around; write a comparison that decides which of two sequence numbers is newer, then extend it to convert incoming numbers into a monotonic 64-bit counter.

Tencent
Software Engineering Fundamentals
Software Engineer
Tencent
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Easy

0

0

0 solved


A real-time game protocol over UDP uses 16-bit sequence numbers that wrap around; write a comparison that decides which of two sequence numbers is newer, then extend it to convert incoming numbers into a monotonic 64-bit counter.

Tencent's mobile shooters and MOBAs synchronise state over UDP-based reliable transports, where compact headers mean small sequence fields that wrap often. A game networking screen uses this to check modular arithmetic, an understanding of why UDP needs ordering logic at all, and attention to the ambiguous half-range case.

What the Interviewer Expects
  • Explains why games prefer UDP plus custom reliability over TCP (head-of-line blocking)
  • Implements serial number comparison with modular subtraction
  • Calls out the exactly-half-range ambiguity
  • Unwraps to a monotonic counter that tolerates late, reordered packets
  • Mentions what the counter is used for (acks, dropping stale snapshots)
Key Topics to Cover
UDP vs TCP
Modular arithmetic
Serial number arithmetic
Packet reordering
Game networking
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you build a 32-packet ack bitfield on top of this?
  • What happens if the sender falls more than half the range behind, for example after a long stall?
  • Why does a stale position snapshot get dropped but a chat packet must be retransmitted?
  • How does this relate to TCP's own sequence comparison?
Sharpen Your Skills on Codemia

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

Practice System Design Problems
Sample Answer
Requirements

Sequence numbers are 16-bit and increment per packet, wrapping from 65535 to 0. seq_newer(a, b) must say whether a was sent after b, assuming packets are never more than half the range (32768) a...

High-Level Design

Why not TCP? A lost TCP segment stalls every later byte until it is retransmitted (head-of-line blocking), which shows as freezes in a 30 Hz game. With UDP the game decides per message whether to rese...


Submit Your Answer
Markdown supported

Related Questions