Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 77 ยท Web and Frontend Interview Questions

Browser, HTTP and Networking Questions

Prepare answers for what happens when you type a URL, HTTP methods and caching, HTTPS, CORS and cookies versus storage, with runnable stand-ins.

"What happens when you type a URL into the browser and press Enter?" is the most famous web interview question, and it is a trapdoor into everything else: DNS, TCP, TLS, HTTP, caching, cookies, rendering. Good candidates give a clear end-to-end story and then go as deep as the interviewer asks.

Real networking cannot be demonstrated honestly in a sandboxed page (no raw sockets, no control of DNS or certificates), so the protocol facts here are explained in text and read-only snippets, and each idea has a stand-in written in JavaScript: a simplified model of the pipeline, a cache, the CORS check and a cookie jar. They illustrate the rules; they are not a browser. For depth see HTTP Basics, HTTP Caching and Cookies, TLS and HTTPS, DNS, Cookies and Web Storage.

What happens when you type a URL

From Enter to pixels

Parse Scheme, host, port, path.
DNS Name becomes an IP address.
Connect TCP (or QUIC), then TLS for https.
Request HTTP request, response comes back.
1 / 2

A model answer, in order:

  1. Parse the input. If it is not a valid URL, it becomes a search query. The browser checks its HSTS list (to upgrade to HTTPS) and caches.
  2. DNS lookup. Browser cache, then the OS cache, then a recursive resolver, which asks root, TLD and authoritative name servers if needed. The result is an IP address (A or AAAA record) with a time-to-live.
  3. Connect. TCP's three-way handshake (SYN, SYN-ACK, ACK). HTTP/3 uses QUIC over UDP instead.
  4. TLS handshake for https: the server proves its identity with a certificate chain that the browser validates, and both sides agree on session keys. Modern TLS 1.3 needs one round trip.
  5. HTTP request and response. For example GET /docs HTTP/1.1 with a Host header and cookies; the server answers with a status line, headers and a body.
  6. Render. Parse the HTML into the DOM, discover and fetch CSS, JS and images, build the CSSOM, compute layout, paint and composite. Scripts without defer or async block parsing; CSS blocks rendering.
HTTP
GET /docs HTTP/1.1
Host: example.com
Accept: text/html
Cookie: session=abc123

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=300

The stand-in adds up simplified costs, with and without caches and connection reuse (the numbers are made up for illustration, not measurements):

JS Stand-in: the cost of a cold versus a warm request

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

No output.

HTTP methods, status codes and idempotency

Q: What is the difference between GET and POST? What is idempotency? GET retrieves a resource and should not change server state (it is safe); responses can be cached and the URL can be bookmarked. POST submits data and usually creates something or triggers an action; it is not safe and not idempotent. An operation is idempotent if doing it many times has the same effect as doing it once. GET, HEAD, PUT and DELETE are idempotent by definition; POST is not, and PATCH is not guaranteed to be. This matters for retries: a network timeout after a POST /payments could mean a double charge unless the API supports an idempotency key.

Method Typical use Safe Idempotent
GET Read a resource yes yes
POST Create or run an action no no
PUT Replace a resource no yes
PATCH Partially update no not guaranteed
DELETE Remove a resource no yes

Status code families: 1xx informational, 2xx success (200 OK, 201 Created, 204 No Content), 3xx redirection (301 permanent, 302/307 temporary, 304 Not Modified), 4xx client error (400, 401 unauthenticated, 403 forbidden, 404, 429 too many requests), 5xx server error (500, 502, 503).

Q: HTTP/1.1 versus HTTP/2 versus HTTP/3? HTTP/1.1 handles one request at a time per connection (browsers open several connections). HTTP/2 multiplexes many requests over one connection and compresses headers, but TCP packet loss still stalls every stream. HTTP/3 runs over QUIC on UDP, so a lost packet only stalls its own stream, and connection setup is faster.

HTTP caching

Q: How does browser caching work? The server sends headers that say how long a response stays fresh and how to revalidate it afterwards:

  • Cache-Control: max-age=300 means reuse it for 300 seconds without asking the server.
  • ETag: "v1" (or Last-Modified) labels a version. When the copy is stale, the browser sends If-None-Match: "v1"; if nothing changed, the server replies 304 Not Modified with no body, saving bandwidth.
  • no-cache does not mean "do not store": it means "store, but revalidate before every use". no-store means do not keep it at all (sensitive data).
  • public allows shared caches such as CDNs; private restricts it to the user's browser.
  • A common production pattern is fingerprinted filenames (app.3f9c2.js) with a very long max-age, and short or no-cache HTML.
JS Stand-in: fresh, stale and revalidated

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

No output.

HTTPS and TLS

Q: What does HTTPS give you, and how does it work? HTTPS is HTTP inside a TLS connection. TLS provides confidentiality (traffic is encrypted), integrity (tampering is detected) and authentication (the browser verifies it is talking to the real domain). During the handshake the server presents a certificate signed by a certificate authority the browser trusts; the two sides then derive shared session keys with an ephemeral key exchange. Bulk data uses fast symmetric encryption; the slower asymmetric cryptography only sets the keys up (and signs for identity). HSTS tells browsers to always use HTTPS for a site. HTTPS hides the content and path from observers but not the destination hostname in all cases, and it does not make the site itself trustworthy.

CORS and the same-origin policy

Q: What is the same-origin policy, and what is CORS? An origin is scheme + host + port. The same-origin policy stops a script on one origin from reading responses from another, so a malicious page cannot read your email just because you are logged in. CORS (Cross-Origin Resource Sharing) is how a server opts in: it sends Access-Control-Allow-Origin naming the origins allowed to read the response. Important details:

  • CORS is enforced by the browser to protect users. curl and servers ignore it, and it is not a server authentication mechanism.
  • "Simple" requests go straight out and only the reading is blocked if the header is missing. Other requests (methods like PUT/DELETE, custom headers, Content-Type: application/json) trigger a preflight: an automatic OPTIONS request asking permission first.
  • Sending cookies cross-origin needs credentials: "include" on the client and Access-Control-Allow-Credentials: true with a specific origin (not *) on the server.
  • You do not fix CORS in the frontend; you configure the server, or proxy the request.
JavaScript
// Read-only: a cross-origin call from https://app.example to https://api.example
const response = await fetch("https://api.example/items", {
  method: "PUT",                                   // not a simple method -> preflight
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "pen" }),
});
JS Stand-in: origins, preflight and the CORS check

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

No output.

Cookies versus localStorage and sessionStorage

Q: Compare cookies, localStorage and sessionStorage.

Cookie localStorage sessionStorage
Sent to the server automatically yes, with matching requests no no
Typical size about 4 KB each about 5 MB per origin about 5 MB per origin
Lifetime until Expires/Max-Age, or the session until cleared until the tab closes
Readable by JavaScript yes, unless HttpOnly yes yes
Scope domain and path origin origin and tab

Cookie attributes are a favorite follow-up: HttpOnly hides the cookie from JavaScript (limits theft through XSS), Secure sends it only over HTTPS, SameSite (Strict, Lax, None) limits cross-site sending and is the main defense against CSRF, and Domain/Path control where it is sent. Store session identifiers in HttpOnly; Secure; SameSite=Lax cookies. Do not keep tokens or secrets in localStorage: any script injected into the page can read it.

HTTP
Set-Cookie: session=abc123; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=3600
JS Stand-in: which cookies are sent with a request?

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

No output.

The last case shows why SameSite helps against CSRF: a form submitted from another site no longer carries the Lax session cookie.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which sequence best matches what happens before the first byte of HTML arrives over HTTPS?

    The name must be resolved first, then a connection is made, then TLS secures it, and only then is the HTTP request sent.
  2. 2. What does Cache-Control: no-cache mean?

    no-store forbids keeping the response. no-cache allows storing but requires revalidation (for example with an ETag) every time.
  3. 3. A page on https://app.example calls https://api.example and the browser blocks reading the response. Where is the usual fix?

    CORS is a server opt-in that the browser enforces. The client cannot grant itself permission.
  4. 4. Which method is NOT idempotent by definition?

    Repeating a POST can create another resource or charge again. PUT, DELETE and GET have the same effect when repeated.
  5. 5. Which is the safest place to keep a session identifier?

    HttpOnly keeps it out of reach of injected scripts, Secure keeps it off plain HTTP, and SameSite reduces CSRF. localStorage is readable by any script on the page.

Summary

  • Typing a URL: parse, DNS, TCP or QUIC, TLS, HTTP request and response, then DOM, CSSOM, layout, paint.
  • GET is safe and idempotent; PUT and DELETE are idempotent; POST is not, so retries need idempotency keys.
  • Caching uses freshness (max-age) and revalidation (ETag with 304); no-cache is not no-store.
  • HTTPS is HTTP over TLS: encryption, integrity and server authentication.
  • CORS is a server opt-in enforced by browsers; preflights ask permission for non-simple requests.
  • Cookies travel with requests and can be HttpOnly, Secure and SameSite; web storage stays in the browser and is readable by any script.

Next: React Interview Questions.