Backend
Database and API: data modelling, SQL, indexes and transactions, plus API design, auth, validation, errors, versioning and operations.
Preparation Q&A
Database · 10
What is the difference between a primary key and a foreign key?
Basic · 1ptExpected answer
A primary key uniquely identifies a row in its own table (unique, not null, one per table). A foreign key is a column that references the primary key of another table, so the database enforces that the referenced row exists.
Explain one-to-many and many-to-many relationships, and how each is stored.
Basic · 1ptExpected answer
One-to-many: the "many" side holds a foreign key to the "one" side (an order has a
customer_id). Many-to-many needs a join table holding two foreign keys (e.g.student_course(student_id, course_id)).What does NULL mean in SQL, and why does `WHERE col = NULL` not work?
Basic · 1ptExpected answer
NULL means "unknown / no value", not zero or empty string. Any comparison with NULL yields unknown, so
= NULLnever matches. UseIS NULL/IS NOT NULL.What is the difference between INNER JOIN and LEFT JOIN? When would you use a LEFT JOIN?
Intermediate · 2ptExpected answer
INNER JOIN returns only rows that match on both sides. LEFT JOIN returns every row from the left table, with NULLs where the right side has no match. Use it to keep rows without a match, e.g. customers with no orders (
WHERE o.id IS NULL).What is the difference between WHERE and HAVING?
Intermediate · 2ptExpected answer
WHEREfilters rows before grouping;HAVINGfilters groups afterGROUP BYand can use aggregates, e.g.HAVING COUNT(*) > 5.What is an index, what does it cost, and when should you add one?
Intermediate · 2ptExpected answer
An index is a separate structure (usually a B-tree) that lets the database find rows without scanning the table. It speeds up reads on filtered/joined/sorted columns but costs disk space and slows every insert/update/delete. Add one for frequent, selective queries — confirmed with the query plan.
What is a transaction, and what does ACID stand for?
Intermediate · 2ptExpected answer
A transaction groups statements so they succeed or fail together. Atomicity (all or nothing), Consistency (constraints hold), Isolation (concurrent transactions do not see each other’s partial work), Durability (committed data survives a crash).
Explain transaction isolation levels and one anomaly that READ COMMITTED allows.
Advanced · 3ptExpected answer
Read Uncommitted, Read Committed, Repeatable Read, Serializable — each prevents more anomalies at more cost. READ COMMITTED allows non-repeatable reads (a row changes between two reads in one transaction) and phantoms (new rows appear in a repeated range query).
A report query became slow in production. How do you investigate and fix it?
Advanced · 3ptExpected answer
Reproduce with the real parameters, run
EXPLAIN ANALYZE, look for sequential scans, bad row estimates, and expensive sorts/joins. Fixes: a suitable (possibly composite) index, rewriting the query, updating statistics, pagination, or pre-aggregating. Measure before and after.How do you run a schema migration on a large live table without downtime?
Advanced · 3ptExpected answer
Make changes backward compatible in steps (expand → migrate → contract): add nullable column, deploy code that writes both, backfill in batches, then add constraints and remove the old column. Avoid long table locks (e.g.
CREATE INDEX CONCURRENTLY), and have a rollback plan and a backup.
API · 10
What are the common HTTP methods and what is each used for in a REST API?
Basic · 1ptExpected answer
GETread,POSTcreate,PUTreplace,PATCHpartial update,DELETEremove. GET must not change state.What do the status code families mean? Give an example of each: 2xx, 4xx, 5xx.
Basic · 1ptExpected answer
2xx success (
200 OK,201 Created,204 No Content); 4xx client error (400 Bad Request,401 Unauthorized,403 Forbidden,404 Not Found,422); 5xx server error (500,503).What is the difference between authentication and authorization?
Basic · 1ptExpected answer
Authentication establishes who you are (login, token). Authorization decides what you are allowed to do (roles, ownership checks). A 401 means not authenticated; a 403 means authenticated but not allowed.
What does idempotent mean? Which HTTP methods are idempotent?
Intermediate · 2ptExpected answer
Repeating the same request has the same effect as sending it once. GET, PUT, DELETE (and HEAD, OPTIONS) are idempotent; POST is not. Matters for safe retries — POST can be made safe with an idempotency key.
Where should input validation happen, and what should the API return for invalid input?
Intermediate · 2ptExpected answer
Always on the server at the boundary, even if the client validates too. Return
400or422with a consistent error body that says which field failed and why — never a 500 or a stack trace.How does token-based authentication with a JWT work, and where are its weaknesses?
Intermediate · 2ptExpected answer
The server signs a token with claims (user id, expiry); the client sends it in the
Authorization: Bearerheader and the server verifies the signature without a session lookup. Weaknesses: hard to revoke before expiry, so keep access tokens short-lived with refresh tokens; never store secrets in the payload; store securely on the client.Compare offset pagination with cursor pagination.
Intermediate · 2ptExpected answer
Offset (
?page=3&limit=20) is simple and allows jumping to a page, but gets slow on large offsets and skips/duplicates rows when data changes. Cursor (?after=<last id>) is fast and stable for feeds, but cannot jump to an arbitrary page. Both need a stable sort order.How would you ship a breaking change to an API that mobile apps already use?
Advanced · 3ptExpected answer
Never break the old contract in place: version it (
/v2or a header), or make the change additive. Run both versions, monitor usage of the old one, communicate a deprecation timeline, and remove it only when old app versions are gone — mobile clients update slowly.How do you protect an API against abuse or traffic spikes?
Advanced · 3ptExpected answer
Rate limiting per user/key/IP (token bucket or sliding window) with
429andRetry-After; request size limits; timeouts; caching; queueing slow work; autoscaling; and monitoring/alerting to spot abuse.A request is slow only sometimes in production. How do you find the cause?
Advanced · 3ptExpected answer
Use logs with correlation/request IDs and metrics (p95/p99 latency) to find which requests and when. Trace the request through its dependencies (DB, external APIs). Common causes: lock contention, N+1 queries, cold caches, slow third parties, resource exhaustion. Reproduce, fix, then confirm with the metrics.