Skip to the content

Lesson 295 of 296

Mock interview 2: back-end, SQL and design round

What you will learn

  • How a back-end round runs
  • Designing a REST API out loud
  • Writing a SQL query under pressure
  • Answering questions on safety, performance and scale
On this page

How the round runs

TimePart
5 minIntroductions
20 minAPI design and back-end questions
15 minSQL task
15 minShort design and scaling discussion
5 minYour questions

Part 1: API design

Interviewer: "Design the API for a simple appointment booking system for a hair salon. Customers book a time slot with a stylist."

Ask questions first:
"Who uses it: customers, stylists, the salon owner? Can customers cancel? Can a stylist have only one booking at a time? Do we take payment?"

Interviewer: "Customers book and cancel. Stylists can't be double-booked. No payments."

Answer, resources first:
"The main resources are stylists, available slots and bookings."

Method and pathPurposeSuccess code
GET /stylistsList stylists200
GET /stylists/{id}/availability?date=2026-12-01Free slots for a day200
POST /bookingsCreate a booking201
GET /bookings/{id}View one booking200
DELETE /bookings/{id} (or POST /bookings/{id}/cancel)Cancel204 or 200

"For POST /bookings the body is { stylistId, startsAt, service }. I'd validate it with a schema, check the user is logged in, and return 409 Conflict if the slot is already taken, 400 for invalid input, and 401 or 403 for auth problems. Errors use one consistent JSON format." (Lessons 194 and 196)

Interviewer: "Two customers click 'book' for the same slot at the same moment. What happens?"
"Checking 'is it free?' in code and then inserting is a race condition: both checks can pass. The database must enforce it. In PostgreSQL I'd use a unique constraint on stylist and start time for fixed slots, or an exclusion constraint for overlapping time ranges. One insert succeeds and the other gets a constraint error, which I turn into a 409." (Lessons 225 and 226)

Interviewer: "The customer's phone loses signal and the app sends the booking twice."
"I'd make POST /bookings idempotent: the app sends an Idempotency-Key header, and the server stores the key with the result. A repeated request with the same key returns the first result instead of creating a second booking." (Lesson 195)

Interviewer: "How do customers get a reminder the day before?"
"Not inside the request. A background job, on a queue with retries, runs on a schedule, finds tomorrow's bookings and sends the messages. If sending fails it retries with backoff, and failed jobs go to a dead-letter queue to check." (Lesson 204)

Part 2: SQL task

Interviewer: "Using these tables, list each stylist's name and number of completed bookings in November 2026, including stylists with zero, busiest first."

SQL
CREATE TABLE stylists (
  id   int PRIMARY KEY,
  name text NOT NULL
);

CREATE TABLE bookings (
  id         int PRIMARY KEY,
  stylist_id int NOT NULL REFERENCES stylists(id),
  starts_at  timestamptz NOT NULL,
  status     text NOT NULL CHECK (status IN ('booked', 'completed', 'cancelled'))
);

INSERT INTO stylists VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Cara');
INSERT INTO bookings VALUES
  (1, 1, '2026-11-03 10:00+00', 'completed'),
  (2, 1, '2026-11-10 11:00+00', 'completed'),
  (3, 2, '2026-11-12 09:00+00', 'completed'),
  (4, 2, '2026-11-15 09:00+00', 'cancelled'),
  (5, 1, '2026-12-01 10:00+00', 'completed');

Think out loud:
"I need every stylist, even with zero, so I start from stylists and LEFT JOIN bookings. The filters on status and date must go in the ON clause, not WHERE; in WHERE they would remove the stylists with no matching bookings and turn it back into an inner join. For the date I use a half-open range: on or after 1 November and before 1 December, which is correct for timestamps and can use an index. COUNT(b.id) counts only matched rows, so stylists with no bookings get 0."

SQL
SELECT s.name, COUNT(b.id) AS completed_bookings
FROM stylists s
LEFT JOIN bookings b
  ON b.stylist_id = s.id
 AND b.status = 'completed'
 AND b.starts_at >= '2026-11-01'
 AND b.starts_at <  '2026-12-01'
GROUP BY s.id, s.name
ORDER BY completed_bookings DESC, s.name;

Result:

namecompleted_bookings
Ana2
Ben1
Cara0

Interviewer: "This table will have millions of rows. What index would help?"
"An index on bookings (stylist_id, starts_at), because the join uses stylist_id and the filter uses a range on starts_at. If most queries filter completed bookings, a partial index with WHERE status = 'completed' keeps it smaller. I'd confirm with EXPLAIN ANALYZE rather than guessing." (Lessons 220 and 221)

Interviewer: "Why COUNT(b.id) and not COUNT(*)?"
"COUNT(*) counts rows, and the LEFT JOIN produces one row for Cara with NULLs, so she would get 1. COUNT(b.id) ignores NULLs, so she correctly gets 0." (Lesson 216)

Part 3: design and scaling discussion

Interviewer: "The salon app becomes a platform for 5,000 salons. What changes?"

A strong answer covers these points in order:

  1. Clarify numbers: "Roughly how many bookings per day, and when are the peaks? Probably mornings and the evening before."
  2. Multi-tenancy: "Every table gets a salon_id, every query filters by it, and access checks make sure staff only see their own salon." (Lesson 203)
  3. Reads: "Availability is read far more than it is written, so I'd cache it briefly in Redis and invalidate the cache when a booking changes." (Lesson 205)
  4. Database: "One well-indexed PostgreSQL database with a read replica will handle a lot. I would not shard until measurements say so." (Lesson 235)
  5. Background work: "Reminders and emails go through a queue, so slow providers do not slow down booking." (Lesson 204)
  6. Reliability: "Health checks, logs, metrics and alerts; backups with tested restores." (Lesson 256)

"I'd start simple and measure, and only add complexity like sharding or extra services when a real limit appears."

How to practise this lesson

Cover the answers and design the API yourself in 15 minutes, out loud, including status codes and the double-booking problem. Then write the SQL query on paper before checking. Practise explaining why the filters belong in the ON clause; this is a very common interview trap.

Key words

WordMeaning
409 ConflictThe request clashes with the current state, such as a taken slot
Idempotency keyA unique key that makes a repeated request safe
Half-open rangeFrom a start (included) to an end (not included)
Partial indexAn index on only the rows matching a condition
Multi-tenancyMany customers (tenants) sharing one system safely

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.