Question library
Explained questions for the decisions developers make.
Which indexes should I know?
An index is a separately maintained structure that makes suitable lookups, joins or ordering operations cheaper. B-tree is a common general-purpose choice, while composite, partial and unique indexes describe field arrangement, row selection and data rules. An index helps only when the planner estimates its use is worthwhile for the query and data.
EXPLAIN vs EXPLAIN ANALYZE?
EXPLAIN shows the execution plan the database expects to use. EXPLAIN ANALYZE actually executes the statement and adds observed rows and timing, which helps reveal bad estimates or expensive steps. Since it runs the statement, write queries and expensive reads need appropriate care.
Is an index scan always faster than a sequential scan?
An index scan follows an index to locate matching rows, while a sequential scan reads the table directly. If a query needs much of a table, following many scattered row locations can cost more than scanning it. PostgreSQL's planner weighs expected row count, I/O and other costs rather than always preferring indexes.
What do VACUUM, ANALYZE and autovacuum do?
PostgreSQL keeps older row versions for concurrent readers, so updates and deletes can leave dead versions behind. VACUUM makes eligible dead space reusable and supports visibility and transaction-ID maintenance; ANALYZE collects statistics for planning queries. Autovacuum runs this maintenance in the background but can be limited by long-running transactions or workload settings.
JSON vs JSONB, and views vs materialized views?
JSON stores JSON input text, while JSONB stores a processed representation that supports efficient operations and indexing. A view stores a query definition, whereas a materialized view stores the query's result for later reads. These are separate choices about data representation and precomputed results.
What are schemas, roles, sequences, functions and triggers?
PostgreSQL schemas organize database objects, and roles with grants control access to those objects. Sequences generate numeric values, functions package executable database logic, and triggers run configured logic in response to events. These features add organization and automation, but hidden trigger behavior can make writes harder to reason about.
What does an ORM do underneath?
An ORM translates object-oriented data operations into database commands and maps results back into application objects. It reduces repetitive persistence code, but the database still executes real SQL with real locks, indexes and query costs. Understanding the generated query is essential when debugging correctness or performance.
How can I practice joins and window functions?
A useful SQL exercise starts with a small dataset whose expected answer can be checked manually. Window functions then demonstrate ranking while keeping each employee's fields available. The example below returns all employees tied for the highest salary in each department.
Props vs state: what is the difference?
Props are values a parent supplies to a component, while state is data React remembers for a component across renders. Both influence the UI, but a component should not change its received props directly. State updates request a new render so the screen can reflect the new data.
What causes a re-render? Does it always change the DOM?
Rendering means React calls components to calculate the next UI description. Committing is the later step where React applies needed changes to the host environment, such as the browser DOM. A component can render again without causing any DOM change if the calculated output is effectively the same.
What are reconciliation, Fiber and keys?
Reconciliation is how React matches elements from the previous and next render to decide what to keep, update or replace. Fiber is the internal representation that helps React organize that work. Keys supply identity among siblings, allowing React to preserve the right state as a list changes.
Controlled vs uncontrolled input?
A controlled input takes its displayed value from React state, with changes flowing through an event handler. An uncontrolled input keeps its current value in the DOM until the application reads it. Controlled inputs make coordinated validation and UI changes convenient, while uncontrolled inputs can keep simple forms less state-heavy.
Why should I use a functional state update?
A functional state update supplies a function that receives the pending previous state and returns the next state. It is useful when several updates may be queued or a callback holds an older render's value. React can apply these updater functions in order without each relying on the same captured value.
When should I use useEffect?
useEffect synchronizes a committed component with something outside React, such as a connection, timer or browser event listener. Its setup runs when relevant dependencies change, and cleanup ends the old synchronization. Effects are not the usual place to compute values already available during rendering.
Why does an effect run twice in development or loop forever?
React Strict Mode performs an extra development-only setup and cleanup cycle for effects to reveal missing cleanup. An infinite effect loop is a different issue, usually caused by updating state that keeps changing an effect dependency. Understanding the trigger helps fix the design rather than masking the behavior.
useRef vs useState?
useState stores a value that participates in the UI, while useRef stores a stable mutable container whose changes do not request rendering. A ref is useful for an imperative handle such as a DOM node or timer ID. It should not replace state for information that the screen must show when it changes.
useMemo vs useCallback?
useMemo caches a calculated value between renders until its dependencies change. useCallback preserves a function reference under the same dependency idea. They can reduce repeated work or unstable props, but memoization is an optimization rather than a correctness guarantee.
When do I use Context, useReducer or a custom hook?
Context shares a value with descendants without passing it through every intermediate component. useReducer organizes state changes through actions and a reducer, while custom hooks extract reusable stateful logic. These tools address distribution, transition structure and reuse respectively; they are not interchangeable state stores.
Difficulty is an editorial estimate. Completion means you chose to mark a question as understood.