TRT
Sign in
easyAdvancedconcept

How does UDP differ from TCP?

Explain connectionless, unreliable, datagram-based delivery versus TCP's reliable byte stream and handshake.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstConnectionless Datagrams: UDP Sockets, Framing, and Packet LossPro — members onlyMaster connectionless UDP networking with sendto and recvfrom, preserve message boundaries, build application-level framing, and diagnose packet loss.

A strong answer

A strong candidate frames How does UDP differ from TCP? around the exact memory layout, compiler semantics, and hardware execution model covered in Connectionless Datagrams: UDP Sockets, Framing, and Packet Loss. Explain connectionless, unreliable, datagram-based delivery versus TCP's reliable byte stream and handshake.

TCP guarantees reliable, ordered byte delivery. However, that reliability introduces structural overhead:

  1. Connection Setup Latency: Every connection requires a 3-way handshake (1 full network Round-Trip Time / RTT) before a single byte of payload can be sent.
  2. Head-of-Line Blocking: If packet 2 of 10 is dropped in transit, the receiving kernel holds packets 3 through 10 in its receive buffer. The application cannot read packet 3 until packet 2 is retransmitted and acknowledged.
  3. No Message Boundaries: TCP is a continuous byte stream. If a sender issues write(sock, "CMD1", 4) and write(sock, "CMD2", 4), the receiver might read all 8 bytes in a single read() call ("CMD1CMD2"), or read 3 bytes ("CMD"), followed by 5 bytes ("1CMD2"). The application must implement stream parsing and delimiter framing manually.

For real-time audio/video streaming, DNS queries, telemetry heartbeats, or high-tick-rate multiplayer game updates, waiting hundreds of milliseconds for TCP retransmissions creates unacceptable latency. A dropped voice audio packet from 200 ms ago is useless; the listener needs the current frame right now.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Instantiate and operate datagram sockets using socket, SOCK_DGRAM, sendto, and recvfrom.
  • Mastery of core invariant: Differentiate message-boundary preservation from continuous byte streams.
  • Mastery of core invariant: Optimize datagram routing and peer filtering by binding endpoints with connect.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How does the compiler, OS kernel, or hardware memory model enforce the invariants of Connectionless Datagrams: UDP Sockets, Framing, and Packet Loss under optimization (`-O2`/`-O3`)?
  2. 02What production bug or undefined behavior occurs when Connectionless Datagrams: UDP Sockets, Framing, and Packet Loss is misused across module or thread boundaries, and which sanitizer (`ASan`, `UBSan`, `TSan`) catches it?

Not started

Sign in to save your learning progress.

Sign in to save