Dev Skills
Go tests·13 questions

Project 3 — Small shop

Roles, file uploads, carts, and transactions that must not half-happen.

1 / 13

A logged-in customer tries to create a product. What status should they get?

All questions with explanations (answers shown)
  1. 1. A logged-in customer tries to create a product. What status should they get?

    Answer: 403 Forbidden

    Authentication passed, so we know who they are, but they may not do this. 401 means we do not know you; 403 means we do, and the answer is still no.

  2. 2. Why does order_items copy the product name and price instead of pointing at the product?

    Answer: So that changing a product’s price does not silently rewrite past orders

    An order must show what was paid at the time. Anything it points at can change; anything it copies cannot.

  3. 3. What must happen before you read a single byte of an upload?

    Answer: http.MaxBytesReader on the body

    Cap the body first, or a 4GB upload takes the process down before any of your other checks run.

  4. 4. How should you decide what type an uploaded file really is?

    Answer: http.DetectContentType on the first 512 bytes

    Extension and header are both set by the client, so both can lie. Sniffing reads the actual magic bytes. It is a type check, not a malware check.

  5. 5. The file is written to disk but the database insert fails. What do you do?

    Answer: os.Remove the file, then return the error

    Otherwise you build up files nothing points at, forever. Clean up on the failure path.

  6. 6. Adding a product already in the cart should increase the quantity. What is the best way?

    Answer: INSERT ... ON CONFLICT (user_id, product_id) DO UPDATE

    One statement, no race between the check and the write. The UNIQUE constraint is what makes it work.

  7. 7. Two customers check out the last item at the same moment. What prevents overselling?

    Answer: Putting the condition in the UPDATE: WHERE id = $2 AND stock >= $1, then checking RowsAffected

    Check-then-update leaves a gap where both requests see "1 left". Putting the condition in the WHERE clause makes the check and the change one statement.

  8. 8. Why does the checkout function use a named return value for its error?

    Answer: So the deferred rollback can see whether an error happened

    defer func() { if err != nil { tx.Rollback() } }() needs to read the error the function is returning. This is one of the few places where naming a return earns its keep.

  9. 9. Which of these must never happen inside a transaction?

    Answer: Calling a payment provider over HTTP

    A transaction holds a pooled connection for its whole life. A slow third party then ties that connection up, and enough of them use up the pool.

  10. 10. Why set X-Content-Type-Options: nosniff when serving uploaded images?

    Answer: It stops the browser guessing the type and running a file you did not mean to be code

    Some browsers ignore your Content-Type and guess from the contents. That guessing is what turns a stored file into script running on your own domain.

  11. 11. Why store uploads behind a small Storage interface with Save, Open and Remove?

    Answer: So swapping local disk for S3 later is a second implementation, not a refactor

    Local disk stops working the moment you run more than one instance. Three methods behind an interface is what makes that a contained change.

  12. 12. What should the stored filename be?

    Answer: A random name you generate with crypto/rand

    Go already strips directory parts from multipart filenames, but a client name can still collide with, and overwrite, another upload, or carry characters your filesystem dislikes. Generate the name yourself and keep the original only as display text.

  13. 13. Why io.Copy(out, file) rather than io.ReadAll?

    Answer: It streams through a small buffer, so memory stays constant regardless of file size

    ReadAll pulls the whole file into memory — ten concurrent 10MB uploads is 100MB of heap. Streaming is what io.Reader/io.Writer exist for.