Computer Networks Interview Questions Explained — TCP, DNS, HTTP and Beyond
- The OSI model is a debugging framework: use it to isolate faults between physical, network, and application layers.
- Reliability (TCP) vs Speed (UDP) is the fundamental trade-off of the transport layer.
- DNS is a distributed, hierarchical database where caching (TTL) is the primary scaling mechanism.
Imagine the internet is a global postal system. Your computer is a house with an address (IP address), the postal routes are the network cables and Wi-Fi signals, and the rules about how letters get packed, addressed, and delivered are the protocols. When you visit google.com, you're essentially writing a letter, dropping it in a mailbox, watching it get sorted through multiple post offices (routers), and getting a reply back — all in milliseconds. Computer networking is the science of making that postal system fast, reliable, and secure.
Every backend engineer, DevOps engineer, and full-stack developer eventually sits across from an interviewer who asks 'What happens when you type a URL into a browser?' That question alone can make or break a senior-level interview. Networking isn't just a theoretical subject — it's the invisible infrastructure that your APIs, databases, and microservices live on. Understanding it deeply separates candidates who just write code from engineers who understand systems.
The OSI Model — Why 7 Layers Actually Matter in Practice
The OSI (Open Systems Interconnection) model is a framework that breaks network communication into 7 distinct layers. Most people memorize the names ('Please Do Not Throw Sausage Pizza Away') and stop there. That's a mistake. Understanding what each layer is responsible for helps you debug real problems.
[Image of the 7 layers of the OSI model]
When your HTTP request fails, is it a DNS issue (Layer 7/5), a TCP connection problem (Layer 4), or a routing issue (Layer 3)? Knowing the layers lets you mentally narrow down where the fault is, just like a doctor using anatomy to diagnose illness.
In practice, you rarely work below Layer 4 (Transport) unless you're writing embedded systems or kernel code. But you absolutely need to understand Layers 3, 4, and 7 — IP addressing, TCP/UDP, and application protocols — because they appear in every production debugging scenario, from a failing API call to a slow database connection.
Here's the critical insight: layers are about separation of concerns. Each layer only talks to the layer directly above and below it. That's why you can swap out Wi-Fi for Ethernet (Layer 1/2 change) without rewriting your HTTP code (Layer 7). The abstraction is intentional and powerful.
package io.thecodeforge.networking; import java.net.InetAddress; import java.net.Socket; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; /** * Demonstration of OSI Layers 3, 4, and 7 in a production Java context. */ public class OsiLayerInspector { public static void main(String[] args) { String host = "example.com"; int port = 80; // Layer 4 (Transport) Port try { // Layer 3 (Network): DNS Resolution InetAddress address = InetAddress.getByName(host); System.out.println("[L3 - Network] Resolved " + host + " to " + address.getHostAddress()); // Layer 4 (Transport): TCP Connection established via Socket try (Socket socket = new Socket(address, port); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { System.out.println("[L4 - Transport] TCP connection established (Handshake complete)"); // Layer 7 (Application): Raw HTTP Protocol communication out.println("GET / HTTP/1.1"); out.println("Host: " + host); out.println("Connection: close"); out.println(); System.out.println("[L7 - Application] HTTP Request Sent"); String responseLine = in.readLine(); System.out.println("[L7 - Application] Server Response: " + responseLine); } } catch (Exception e) { System.err.println("Connection Failed at specific layer: " + e.getMessage()); } } }
[L4 - Transport] TCP connection established (Handshake complete)
[L7 - Application] HTTP Request Sent
[L7 - Application] Server Response: HTTP/1.1 200 OK
TCP vs UDP — Choosing the Right Delivery Guarantee
TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the two workhorses of the Transport layer, and choosing between them is one of the most consequential decisions in system design.
TCP is like sending a package with signature confirmation. Before any data moves, there's a 3-way handshake (SYN, SYN-ACK, ACK). Every packet is numbered, acknowledged, and retransmitted if lost. Order is guaranteed. This reliability costs time — that handshake adds latency, and the acknowledgment mechanism adds overhead.
UDP is like dropping a flyer through every door in the neighbourhood. You send it and forget it. No handshake, no acknowledgment, no guarantee of delivery or order. But it's blazingly fast, which is exactly what you need for real-time applications.
In modern systems, QUIC (used by HTTP/3) is effectively UDP with reliability built on top of it — proof that the TCP/UDP choice isn't always binary.
package io.thecodeforge.networking; import java.net.*; import java.nio.charset.StandardCharsets; public class ProtocolComparison { // TCP: Reliable delivery for sensitive data public void tcpTransmission(String message) throws Exception { try (Socket socket = new Socket("localhost", 9001)) { socket.getOutputStream().write(message.getBytes()); } } // UDP: Unreliable but fast for high-frequency updates (gaming/telemetry) public void udpTransmission(String message) throws Exception { try (DatagramSocket socket = new DatagramSocket()) { byte[] buf = message.getBytes(StandardCharsets.UTF_8); DatagramPacket packet = new DatagramPacket( buf, buf.length, InetAddress.getByName("localhost"), 9002 ); socket.send(packet); } } }
// UDP: Sent packet to network buffer without verification.
DNS Deep Dive — What Actually Happens When You Type a URL
DNS (Domain Name System) is the internet's phonebook. You know the name (google.com), and DNS finds the phone number (IP address). But the process behind that lookup is more fascinating than most people realise — and it's a classic interview question.
When your browser needs to resolve 'api.github.com', it doesn't just ask one server. It walks a hierarchy. First, it checks its local cache. If that's empty, it asks your OS's resolver. If that misses, it queries your ISP's recursive resolver. That resolver then walks the DNS tree: it asks a Root Name Server for the authoritative server for '.com', then asks that server for 'github.com', then finally asks GitHub's authoritative DNS server for 'api.github.com'. The answer comes back and gets cached at every step.
# Using 'dig' to trace the iterative resolution process (standard interview tool) # Trace github.com from the root servers down dig +trace github.com # Inspect the TTL (Time To Live) to understand caching behavior dig github.com | grep "IN A"
github.com. 60 IN A 140.82.121.4
HTTP vs HTTPS, Status Codes, and Subnetting — The Interview Essentials
These three topics appear in virtually every networking interview, so let's cover them with precision.
HTTP vs HTTPS: HTTP sends everything in plaintext. HTTPS wraps HTTP inside TLS (Transport Layer Security). The TLS handshake happens after the TCP handshake. After that, all data is encrypted.
HTTP Status Codes: These are a language. 2xx means success. 3xx means redirect. 4xx means the client made an error. 5xx means the server failed.
Subnetting: An IP address like 192.168.1.100/24 means the first 24 bits identify the network and the last 8 bits identify the host. /24 gives you 256 addresses (254 usable).
package io.thecodeforge.networking; /** * Simulating CIDR mask logic for interview discussions. */ public class SubnetCalculator { public static void main(String[] args) { int prefix = 24; int totalHosts = (int) Math.pow(2, (32 - prefix)); int usableHosts = totalHosts - 2; // Subtract Network and Broadcast System.out.println("CIDR /" + prefix + " allows for " + usableHosts + " usable hosts."); } }
| Aspect | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented (3-way handshake) | Connectionless (no handshake) |
| Reliability | Guaranteed delivery & ordering | No delivery guarantee, no ordering |
| Speed | Slower (overhead from ACKs) | Faster (fire and forget) |
| Error Checking | Full — retransmits lost packets | Checksum only — no retransmission |
| Use Cases | HTTP, HTTPS, SSH, FTP, SMTP | DNS, video streaming, VoIP, gaming |
| Header Size | 20–60 bytes | 8 bytes fixed |
| Flow Control | Yes (sliding window) | No |
| Congestion Control | Yes (slow start, AIMD) | No — app must handle it |
| HTTP Version | HTTP/1.1, HTTP/2 | HTTP/3 (via QUIC) |
🎯 Key Takeaways
- The OSI model is a debugging framework: use it to isolate faults between physical, network, and application layers.
- Reliability (TCP) vs Speed (UDP) is the fundamental trade-off of the transport layer.
- DNS is a distributed, hierarchical database where caching (TTL) is the primary scaling mechanism.
- HTTPS is TLS-wrapped HTTP; the security happens after the TCP connection is established.
- Subnetting is the primary tool for network isolation and IP management in modern cloud architectures.
⚠ Common Mistakes to Avoid
Interview Questions on This Topic
- QWhat is the difference between an IP address and a MAC address, and at which OSI layers do they operate?
- QExplain the 'Head-of-Line Blocking' problem in TCP and how HTTP/3 (QUIC) solves it.
- QDescribe the full lifecycle of an HTTP request, starting from the DNS lookup to the TCP FIN packet.
- QWhat is MTU (Maximum Transmission Unit), and what happens when a packet exceeds the MTU of a router along its path?
- QHow does a Load Balancer (Layer 4 vs Layer 7) differ in how it handles incoming traffic?
Frequently Asked Questions
What is 'Anycast' routing in DNS?
Anycast allows multiple physical servers to share the same IP address. Routers then direct traffic to the 'closest' instance based on network topology. This is how the 13 Root DNS servers handle global traffic and DDoS attacks effectively.
Why do we say TCP has 'Congestion Control' but UDP doesn't?
TCP monitors network health. If it detects packet loss, it slows down its transmission rate (Slow Start/Congestion Avoidance). UDP doesn't care; it will continue to blast data even if the network is saturated, meaning the application must handle throttling itself.
What is the 'Default Gateway'?
The Default Gateway is the router that your computer sends traffic to when the destination IP is not in the local subnet. It is essentially the 'exit' out of your local network into the broader internet.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.