Dev Skills
Go tests·8 questions

Project 1 — URL shortener

Validation, random codes, redirects, clashes, and a Postgres store.

1 / 8

A user submits javascript:alert(1) to shorten. url.Parse returns no error. What should the service do?

All questions with explanations (answers shown)
  1. 1. A user submits javascript:alert(1) to shorten. url.Parse returns no error. What should the service do?

    Answer: Reject it: only allow http or https schemes, with a non-empty host

    url.Parse accepts many strings that are not usable web links. Checking the scheme and host is what stops the shortener redirecting people to script URLs.

  2. 2. Why are codes generated with crypto/rand and not math/rand?

    Answer: Codes from math/rand are predictable, so someone could guess links that have not been shared yet

    math/rand is not designed to be unpredictable. For anything a stranger should not guess, use crypto/rand.

  3. 3. The alphabet leaves out look-alike characters such as 0, O, 1, l and I. Why?

    Answer: People read and type codes by hand, and look-alikes cause wrong links

    Every character is valid in a URL path. Leaving out characters people confuse makes codes safe to read aloud or copy from paper, at the cost of a slightly smaller alphabet.

  4. 4. Two requests generate the same random code at the same moment. What actually stops both being saved?

    Answer: The UNIQUE constraint on code, with the duplicate-key error turned into ErrCodeTaken and the save retried with a new code

    A check-then-insert has a gap between the two steps. Only the database constraint decides atomically. The service retries a few times on ErrCodeTaken and treats any other error as a real failure.

  5. 5. The redirect sends 301 Moved Permanently. Later the owner edits where the link points. What happens to people who already visited?

    Answer: Their browsers may keep going to the old destination, because a 301 is cached hard

    Browsers cache a 301 aggressively, often with no expiry. Use 302 unless the link can never change. It also lets you count clicks, because every visit reaches your server.

  6. 6. Where is the Store interface that the service uses declared?

    Answer: In the service package, the one that uses it, with only the methods it needs

    Go interfaces are declared by the consumer. A small interface in the service package means the memory store, the Postgres store and a test fake all satisfy it without importing each other.

  7. 7. The Postgres store gets sql.ErrNoRows when looking up a code. What should it return?

    Answer: The model's own ErrNotFound, the same error the memory store returns

    Translate driver errors at the storage boundary. The handler then checks errors.Is(err, model.ErrNotFound) without knowing which store is behind it.

  8. 8. The create handler reads JSON with json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)). What does MaxBytesReader protect against?

    Answer: A client sending a huge body to use up memory

    It stops reading after the limit and returns an error, so one request cannot make the server buffer gigabytes. Slow headers are what ReadHeaderTimeout is for.