Skip to content
Tutorials Center
My learning

Topics

Lesson 51 of 77 ยท CS Fundamentals Interview Questions

Computer Networks: OSI, TCP, DNS and Subnetting

Answer networking interview questions: OSI versus TCP/IP, TCP handshake and teardown, TCP versus UDP, congestion control, DNS, NAT and subnet calculations.

Networking questions arrive in two forms. Software roles ask "what happens when two programs talk over TCP?" and "why is my request slow?". Campus and infrastructure roles ask for definitions and calculations: "name the OSI layers", "how many hosts are in a /26?". The best answers combine both: a precise definition, then a concrete example from something you have run.

The browser-level story (URL to page, HTTP caching, CORS, TLS) is in Browser, HTTP and Networking Questions, and security topics are in Security and Cryptography Questions. This lesson covers the lower layers: the layered models, TCP and UDP, flow and congestion control, DNS, addressing and subnetting. Longer treatments are in the Computer Networks tutorial. Examples run in the Python runner (subnet maths, a congestion simulation) and the Node runner (real TCP and UDP sockets on loopback).

The TCP/IP layers

Application HTTP, DNS, SMTP.
Transport TCP and UDP, ports.
Internet IP addresses and routing.
Link Ethernet, Wi-Fi, MAC addresses.

Each layer only talks to its neighbours.

1 / 2

What are the OSI and TCP/IP models, and how do they differ?

Short answer: Both describe a stack of layers where each layer serves the one above and uses the one below. The OSI model has seven layers, and the TCP/IP model (the one the internet actually uses) has four or five.

OSI layer Job TCP/IP layer Examples
7 Application services for programs Application HTTP, DNS, SMTP
6 Presentation encoding, compression, encryption Application TLS, JSON
5 Session dialogue control Application sessions, RPC
4 Transport process-to-process delivery Transport TCP, UDP
3 Network host-to-host routing Internet IP, ICMP
2 Data link node-to-node on one link Link Ethernet, Wi-Fi, ARP
1 Physical bits on a medium Link cables, radio

The unit of data has a name at each layer: segment (TCP) or datagram (UDP) at transport, packet at network, frame at link. Each layer encapsulates the data from above with its own header. Be ready to add that OSI is a teaching reference model while TCP/IP is the working protocol suite, and that devices map to layers: a hub works at layer 1, a switch at layer 2 (forwards by MAC address), a router at layer 3 (forwards by IP address).

How does TCP set up and tear down a connection?

Short answer: TCP is connection-oriented and reliable. It opens with a three-way handshake and closes with a four-step teardown.

  1. SYN: the client sends its initial sequence number.
  2. SYN-ACK: the server acknowledges it and sends its own sequence number.
  3. ACK: the client acknowledges the server's number. Data can now flow in both directions.

The handshake agrees on initial sequence numbers and confirms both sides can send and receive. Teardown uses FIN and ACK in each direction (so four segments), because each side closes its sending half independently. The side that closes first enters TIME_WAIT for about twice the maximum segment lifetime, so late packets from the old connection cannot be mistaken for a new one.

TCP's reliability comes from sequence numbers, acknowledgements, retransmission on timeout, checksums, and in-order delivery. The next demo opens a real TCP connection and a UDP exchange on the loopback interface and shows what differs.

Node.js TCP connection versus UDP datagrams, on loopback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The TCP server saw one continuous byte stream, with no boundaries between the three writes. UDP kept each message separate. On loopback UDP rarely loses anything, so the output looks tidy; on a real network, delivery and order are not guaranteed, which is exactly the trade-off interviewers ask about.

TCP or UDP?

Short answer: Use TCP when every byte must arrive correctly and in order (web pages, email, file transfer, databases). Use UDP when low latency matters more than perfect delivery or when the application provides its own reliability (DNS lookups, voice and video calls, online games, QUIC for HTTP/3).

TCP UDP
Connection handshake first none
Delivery reliable, ordered, no duplicates best effort
Boundaries byte stream datagrams
Overhead larger header (20 bytes minimum), state 8-byte header
Control flow and congestion control none

Flow control versus congestion control

Short answer: Flow control stops a fast sender from overwhelming a slow receiver, using the receiver's advertised window. Congestion control stops senders from overwhelming the network, using a congestion window (cwnd) that the sender maintains itself. The sender may have at most min(receiver window, cwnd) bytes in flight.

The classic congestion algorithm has two phases. Slow start doubles cwnd every round trip from 1 segment until it reaches the threshold ssthresh. Congestion avoidance then adds one segment per round trip (additive increase). When a loss is detected, ssthresh is set to half of cwnd. In the older Tahoe version cwnd drops back to 1; in Reno, a loss signaled by three duplicate ACKs halves cwnd instead of resetting it (multiplicative decrease). This is AIMD, additive increase and multiplicative decrease.

Python A congestion window, round trip by round trip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the shape: exponential growth to 16, linear growth after, then a collapse at round 12 and a new slow start with a lower threshold (half of the window at the loss). Draw this sawtooth on a whiteboard and you have answered the question.

What happens in a DNS lookup?

Short answer: DNS maps names to IP addresses. A program asks its stub resolver, which asks a recursive resolver (usually your ISP's or a public one). If the answer is not cached, the recursive resolver walks the hierarchy: a root server points to the TLD server (.com), which points to the domain's authoritative server, which returns the record. The answer is cached for its TTL.

Know the record types: A (name to IPv4), AAAA (IPv6), CNAME (alias to another name), MX (mail servers), NS (name servers), TXT (free text, often for verification and email policy). DNS normally uses UDP port 53, and TCP for large responses and zone transfers.

How do IP addresses and subnet masks work?

Short answer: An IPv4 address has 32 bits, split into a network part and a host part by the subnet mask. In CIDR notation /n means the first n bits are the network. A /26 leaves 6 host bits, so each subnet has 2โถ = 64 addresses, of which 62 are usable (the all-zeros address is the network address, the all-ones is the broadcast).

The recipe for "which subnet is 192.168.10.77/26 in?": the block size is 256 โˆ’ 192 = 64, so subnets start at 0, 64, 128, 192, and 77 falls in the block starting at 64. The network is 192.168.10.64, the broadcast 192.168.10.127, the usable range .65 to .126.

Python Subnet calculations with the ipaddress module

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What are NAT, ARP and DHCP?

  • NAT (network address translation) lets many hosts with private addresses (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) share one public address. The router rewrites addresses and ports and keeps a table to send replies back. It delayed IPv4 exhaustion but breaks end-to-end addressing; IPv6 (128-bit addresses) removes the need.
  • ARP (address resolution protocol) finds the MAC address that goes with an IP address on the local network. The host broadcasts "who has 192.168.1.1?" and the owner replies.
  • DHCP (dynamic host configuration protocol) hands out an IP address, mask, default gateway and DNS server automatically. The exchange is Discover, Offer, Request, Acknowledge.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which device forwards frames using MAC addresses?

    A switch works at layer 2 and forwards by MAC address. A hub repeats bits at layer 1; a router forwards by IP address at layer 3.
  2. 2. How many usable host addresses does a /26 subnet have?

    A /26 has 6 host bits: 64 addresses. The network and broadcast addresses are not usable, leaving 62.
  3. 3. What is the difference between flow control and congestion control?

    The receiver's window limits what the receiver can take; cwnd limits what the network can take. The sender uses the smaller of the two.
  4. 4. Which sequence correctly describes the TCP three-way handshake?

    The client sends SYN, the server replies SYN-ACK, and the client finishes with ACK.
  5. 5. Why is DNS usually carried over UDP?

    DNS queries are tiny, so UDP avoids the connection setup. DNS falls back to TCP for large responses and zone transfers.

Summary

  • OSI has seven layers, TCP/IP four or five; hubs, switches and routers work at layers 1, 2 and 3.
  • TCP: connection with a three-way handshake, reliable ordered bytes, four-step teardown. UDP: connectionless datagrams.
  • Flow control protects the receiver; congestion control (slow start, then additive increase and multiplicative decrease) protects the network.
  • DNS resolution goes stub resolver, recursive resolver, root, TLD, authoritative, with answers cached for the TTL.
  • A /n subnet has 2^(32โˆ’n) addresses and two fewer usable ones; find the block by the mask's block size.
  • NAT shares one public address, ARP maps IP to MAC, DHCP configures hosts automatically.

Next: Interview Puzzles and Brain Teasers.