December Code

DNS and HTTP interview questions, with answers

Application-layer questions are where networking interviews meet web development: how a domain name becomes an address, what a browser does to load a page, and how HTTP works. "What happens when you type a URL?" in particular is asked everywhere, because it touches every layer.

The answers below cover DNS resolution, the journey of a URL, HTTP versus HTTPS, request methods, status codes, and cookies with HTTP's statelessness. Then take the free Computer Networks diagnostic — ten questions across every networking topic in the bank — to see which of these you can explain but not yet apply.

The questions, with answers

  1. 1.How does DNS resolve a domain name to an IP address?

    In short: A resolver asks the root, then the top-level-domain servers, then the domain's authoritative server, caching each answer.

    The browser and operating system first check their caches. If the answer is not cached, the stub resolver asks a recursive resolver, often run by the ISP or a public service. That resolver asks a root server, which refers it to the servers for the top-level domain such as .com; those refer it to the domain's authoritative name servers, which return the record, for example an A record with an IPv4 address or AAAA for IPv6. The resolver caches every answer for its time to live, so most lookups are answered from cache in milliseconds.

  2. 2.What happens when you type a URL into a browser?

    In short: The browser resolves the name with DNS, opens a TCP (and TLS) connection, sends an HTTP request, and renders the response.

    The browser parses the URL and resolves the host name to an IP address through DNS. It opens a TCP connection to that address on port 443, performs a TLS handshake that authenticates the server's certificate and agrees on encryption keys, and sends an HTTP request for the path. Along the way, ARP finds the gateway's MAC address and routers forward the packets hop by hop. The server returns a response with a status code, headers and the HTML, which the browser parses, fetching the stylesheets, scripts and images it references, before laying out and painting the page.

  3. 3.What is the difference between HTTP and HTTPS?

    In short: HTTPS is HTTP carried over TLS, which encrypts the traffic, protects its integrity and authenticates the server with a certificate.

    Plain HTTP sends requests and responses as readable text, so anyone on the path can read or modify them. HTTPS runs the same HTTP inside a TLS connection, normally on port 443. The TLS handshake verifies the server's certificate, issued by a certificate authority the browser trusts, which proves the site's identity, and establishes session keys, after which all traffic is encrypted and integrity-checked. HTTPS protects passwords, cookies and page content from eavesdropping and tampering, and browsers now mark plain HTTP pages as not secure.

  4. 4.What is the difference between GET and POST?

    In short: GET retrieves a resource with parameters in the URL and should have no side effects; POST sends data in the body to create or process something.

    GET requests a representation of a resource; any parameters go in the URL's query string, so they appear in logs and history, and GET is defined as safe and idempotent, meaning it should not change server state and repeating it has the same effect. That lets browsers and proxies cache GET responses and retry them. POST sends data in the request body to be processed, such as submitting a form or creating an order; it is neither safe nor idempotent, so browsers warn before resubmitting. PUT, PATCH and DELETE cover replacing, partly updating and removing resources.

  5. 5.What do the HTTP status code classes mean?

    In short: 1xx informational, 2xx success, 3xx redirection, 4xx client error and 5xx server error.

    The first digit of a status code gives its class. 1xx codes are informational, such as 101 Switching Protocols for WebSockets. 2xx means success: 200 OK, 201 Created, 204 No Content. 3xx means redirection or using a cached copy: 301 and 308 for permanent moves, 302 and 307 for temporary ones. 4xx means the client made an error: 400 Bad Request, 401 Unauthorized (not authenticated), 403 Forbidden (authenticated but not allowed), 404 Not Found and 429 Too Many Requests. 5xx means the server failed: 500 Internal Server Error, 502 Bad Gateway and 503 Service Unavailable.

  6. 6.What are cookies, and why does HTTP need them?

    In short: HTTP is stateless, so servers set cookies that the browser returns with each request, letting them recognise sessions.

    Each HTTP request is independent: the protocol itself remembers nothing between requests. To keep a user logged in or a cart filled, the server sends a Set-Cookie header, and the browser stores the cookie and sends it back in the Cookie header of later requests to the same site, typically carrying a session identifier. Attributes control its behaviour: Expires or Max-Age for lifetime, Secure for HTTPS only, HttpOnly to hide it from JavaScript, and SameSite to limit cross-site sending, which helps against cross-site request forgery.

How the diagnostic asks it

One question from the Computer Networks bank, exactly as a sitting would show it. The bank has 4 on application layer and 30 across Computer Networks.

Application Layer · mediumCN-022

A browser requests a cached page again, sending If-None-Match with the ETag it stored. The page has not changed. What does the server return?

  1. 1200 OK, with the full page
  2. 2301 Moved Permanently
  3. 3304 Not Modified, with no bodycorrect
  4. 4412 Precondition Failed

If-None-Match makes the request conditional: if the resource's current ETag matches the one the browser sent, the server answers 304 Not Modified with no body, and the browser uses its cached copy, saving bandwidth. 200 with the full page is the reply when the ETag no longer matches. 301 means the resource has a new permanent URL, which is unrelated to caching. 412 Precondition Failed answers a failed If-Match, which guards writes, not a matching If-None-Match on a GET.

Measure it

Reading answers tells you what’s true. A diagnostic tells you what you get wrong.

10 Computer Networks questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

What the readiness test measures · how the score is computed

By Harshit · updated