Staff-Level Frontend Developer Interview Preparation Guide for Spotify
Spotify's staff-level frontend developer interview process typically spans 4-6 weeks and consists of multiple phases: initial recruiter screening, technical phone interviews to assess coding proficiency and system design thinking, and comprehensive onsite interviews evaluating technical depth, architectural thinking, leadership capabilities, and cultural alignment. Staff-level candidates are expected to demonstrate mastery of frontend technologies, experience architecting large-scale systems, mentorship of junior engineers, and strategic thinking about technical direction.
Interview Rounds
Recruiter Screening
What to Expect
Initial conversation with a recruiter to discuss your background, career trajectory, salary expectations, and alignment with Spotify's culture and values. The recruiter will provide an overview of the role, team structure, and upcoming interview process. This round is non-technical and serves as a mutual fit assessment.
Tips & Advice
Be specific about your achievements and impact. Use metrics when possible (e.g., 'I reduced page load time by 40%'). Research Spotify's current challenges and express genuine interest in their mission. For a staff-level role, highlight your experience with large-scale systems and team leadership. Ask thoughtful questions about the team, technical challenges, and growth opportunities.
Focus Topics
Staff-Level Impact Examples
Specific examples of mentoring, architectural decisions, and cross-functional influence you've had in previous roles
Practice Interview
Study Questions
Career Narrative and Growth Trajectory
Your professional journey emphasizing progression toward staff-level expertise, key projects, and evolution of technical leadership
Practice Interview
Study Questions
Spotify Alignment and Mission Understanding
Knowledge of Spotify's business, engineering culture, technical challenges, and how your expertise aligns with their needs
Practice Interview
Study Questions
Technical Phone Screen 1: Coding and Problem-Solving
What to Expect
First technical phone interview focused on coding proficiency and algorithmic thinking. You'll solve 1-2 medium-to-hard frontend or general coding problems using a shared coding environment. The interviewer will assess your problem-solving approach, code quality, ability to optimize, and communication skills. At the staff level, interviewers also evaluate how you think about system-level implications of your solutions.
Tips & Advice
Start by clarifying requirements and asking clarifying questions before coding. Walk through your approach before implementing. For staff-level candidates, discuss trade-offs and implications of your solution. Even if you complete the problem quickly, discuss optimizations, edge cases, and how this would fit into a larger system. Be prepared to explain why you chose certain approaches over alternatives. Code should be clean, well-structured, and production-ready.
Focus Topics
Problem Decomposition and Communication
Ability to break down complex problems, communicate your thinking clearly, and walk interviewers through your approach
Practice Interview
Study Questions
Code Optimization and Trade-offs
Identifying opportunities for optimization, understanding readability vs performance trade-offs, and choosing pragmatic solutions
Practice Interview
Study Questions
JavaScript Advanced Concepts
Mastery of closures, prototypal inheritance, async/await, event loop, memory management, and advanced ES6+ features
Practice Interview
Study Questions
Algorithm and Data Structure Mastery
Deep understanding of algorithms, data structures, and their trade-offs. Ability to analyze time and space complexity and select optimal approaches
Practice Interview
Study Questions
Technical Phone Screen 2: Frontend Architecture and Patterns
What to Expect
Second technical phone interview focused on frontend-specific expertise, architectural patterns, and system-level frontend thinking. You'll be asked to discuss how you'd structure large frontend applications, handle state management, optimize performance, and make technology choices. This may involve live coding a small component or architecture discussion, but the emphasis is on architectural thinking and decision-making.
Tips & Advice
Prepare detailed examples of frontend systems you've designed or contributed to. Be ready to justify technology choices (e.g., why Redux over Context API, or vice versa, for a specific use case). Discuss performance optimization strategies you've implemented. At the staff level, interviewers expect you to understand trade-offs deeply and consider scalability, maintainability, and team productivity. Use concrete metrics from past projects when possible.
Focus Topics
Frontend Scalability and Architecture Decisions
Building systems that scale with team size and codebase complexity. Module boundaries, component hierarchies, and maintaining code quality over time
Practice Interview
Study Questions
Frontend Performance Optimization
Techniques including code splitting, lazy loading, caching strategies, bundle optimization, Core Web Vitals, and profiling tools
Practice Interview
Study Questions
State Management at Scale
Experience with state management solutions (Redux, MobX, Zustand, etc.), understanding trade-offs between approaches, and designing state architecture for complex applications
Practice Interview
Study Questions
React Advanced Patterns and Optimization
Deep expertise in React including hooks, render optimization, suspense, concurrent features, and common pitfalls. Understanding when and why to use each pattern
Practice Interview
Study Questions
Onsite Interview 1: System Design for Large-Scale Frontend
What to Expect
Deep dive into frontend system design at scale. You'll be asked to design a complex frontend system similar to what Spotify builds—such as music streaming UI, recommendation UI, playlist management system, or real-time notification system. The focus is on architectural decisions, component design, state management, performance considerations, and how to handle real-time updates or complex interactions. You're expected to scope the problem, make clear assumptions, and drive the discussion with confidence.
Tips & Advice
Start by scoping the problem and clarifying requirements with the interviewer. Ask about scale (users, data volume, QPS). Make assumptions explicit. Draw diagrams of your component architecture, data flow, and state management. Discuss trade-offs between different approaches (client-side vs server-side rendering, caching strategies, etc.). For staff-level, interviewers expect you to think about scalability, performance budgets, and how this integrates with backend systems. Be ready to drill down into details or zoom out based on interviewer cues. Mention specific technologies or patterns you've used in the past.
Focus Topics
Component Design and Composition Patterns
Designing reusable, composable components; compound components, render props, custom hooks, and avoiding prop drilling
Practice Interview
Study Questions
Caching and Data Fetching Strategies
Client-side caching, HTTP caching, cache invalidation, SWR patterns, and optimistic updates for fast user experiences
Practice Interview
Study Questions
Real-Time Data and WebSocket Integration
Handling real-time updates, push notifications, WebSocket connections, and keeping frontend state synchronized with backend changes
Practice Interview
Study Questions
Large-Scale Frontend Architecture
Designing modular, scalable component hierarchies and system architecture for complex applications with millions of users
Practice Interview
Study Questions
Onsite Interview 2: Advanced Coding Challenge
What to Expect
A more complex coding challenge than phone screens, often involving building a small feature or fixing a system with multiple components. May involve implementing a UI component with specific requirements, optimizing code, or solving a problem that requires both algorithmic thinking and frontend knowledge. Some companies use AI-assisted environments here; you may be expected to explain and justify any AI-generated code you use.
Tips & Advice
Read the problem thoroughly and ask clarifying questions. Break the problem into smaller sub-problems. Write clean, maintainable code with proper error handling. If you use AI assistance, make sure you fully understand every line and be ready to explain trade-offs. Test your code mentally and discuss edge cases. At staff level, code should demonstrate best practices: proper abstraction, reusability, and consideration for how it integrates into a larger system. Don't over-engineer, but show thoughtful design decisions.
Focus Topics
Code Quality and Best Practices
Writing maintainable code, following SOLID principles in frontend context, proper naming, avoiding common pitfalls
Practice Interview
Study Questions
Debugging and Problem-Solving
Systematic approach to finding root causes, using browser dev tools effectively, and reasoning through complex issues
Practice Interview
Study Questions
React Component Implementation and Hooks
Building production-quality React components using hooks, managing side effects, and writing testable component code
Practice Interview
Study Questions
Onsite Interview 3: Technical Leadership and Architecture Discussion
What to Expect
Conversation with a senior engineer or architect about your experience leading technical initiatives, making architectural decisions, and mentoring others. You'll discuss a significant project you led or contributed to at an architectural level. The focus is on your decision-making process, how you navigate trade-offs, how you've influenced technical direction, and how you work with teams to implement complex changes.
Tips & Advice
Prepare 2-3 detailed examples of technical leadership situations: initiating a migration, introducing new technology, refactoring a major system, mentoring junior engineers through a complex task, or resolving a significant architectural debt. Use the STAR method but focus on your decision-making process and how you influenced outcomes. Discuss what you learned, what you'd do differently, and how this experience prepared you for staff-level responsibilities. Be honest about failures and what you learned from them. Staff-level interviewers expect maturity and self-awareness.
Focus Topics
Project Leadership and Execution
Planning and executing complex technical projects, breaking them into phases, managing risks, and delivering on commitments
Practice Interview
Study Questions
Cross-Functional Collaboration
Working effectively with designers, product managers, backend engineers, and other teams to achieve shared goals
Practice Interview
Study Questions
Mentorship and Influence
Experience mentoring junior engineers, raising team capability, and influencing technical direction through ideas rather than authority
Practice Interview
Study Questions
Technical Decision-Making and Trade-offs
Framework for making architectural decisions, evaluating options, considering business impact, and communicating reasoning to stakeholders
Practice Interview
Study Questions
Onsite Interview 4: Behavioral and Cultural Fit
What to Expect
Interview focused on your values, how you work with teams, your approach to challenges, and alignment with Spotify's culture. You'll discuss your work style, how you handle disagreement, your approach to continuous learning, and past experiences working in team environments. This assesses whether you'll thrive in Spotify's culture and contribute positively to the team.
Tips & Advice
Prepare stories about collaboration, handling conflicts, learning from mistakes, and adapting to new situations. Be authentic—interviewers can detect rehearsed answers. Research Spotify's culture and values; understand what they stand for. Discuss what attracts you to working there. For staff level, emphasize how you've built strong teams, handled ambiguity, and maintained perspective during challenges. Ask thoughtful questions about team dynamics and engineering culture.
Focus Topics
Handling Ambiguity and Complexity
How you approach unclear requirements, scope management, and navigating competing priorities
Practice Interview
Study Questions
Growth Mindset and Learning
Your approach to staying current with technology, learning from failures, and continuously improving
Practice Interview
Study Questions
Collaboration and Teamwork
Your approach to working with diverse team members, giving and receiving feedback, and contributing to team success
Practice Interview
Study Questions
Onsite Interview 5: Executive or Senior Leadership Conversation
What to Expect
Optional final round with a director, VP, or senior leadership member. This is often more conversational and focuses on your career vision, long-term goals, and how you see your role contributing to Spotify's technical strategy. It's also an opportunity for you to ask about organizational direction, team structure, and opportunities for growth. This round is also an assessment of executive presence and communication skills at a higher level.
Tips & Advice
Be articulate about your vision for frontend engineering and where the field is heading. Discuss your career trajectory and where staff level fits in your long-term goals. Ask about Spotify's technical strategy, challenges they're facing, and how frontend fits into their roadmap. Show that you think about the business, not just code. Be confident but not arrogant. This person is assessing whether you're someone they'd want on their leadership team.
Focus Topics
Communication with Leadership
Ability to communicate complex technical topics to non-technical stakeholders and translate business needs to technical strategy
Practice Interview
Study Questions
Strategic Thinking and Business Acumen
Understanding how technical decisions impact business, thinking about trade-offs beyond engineering, and contributing to strategy
Practice Interview
Study Questions
Career Vision and Long-Term Goals
Your vision for your role at staff level and beyond, how it aligns with Spotify, and what success looks like
Practice Interview
Study Questions
Frequently Asked Frontend Developer Interview Questions
Compare localStorage, sessionStorage, cookies, and IndexedDB for client-side caching. For each API list ideal use-cases, approximate size limits, persistence characteristics, and key security considerations for storing cached data.
Sample Answer
Comparison
| API | Ideal use case | Approximate size limit | Persistence | Key security considerations |
|---|---|---|---|---|
localStorage | Small key-value settings/preferences that should survive closing the browser, such as a theme choice | About 5 to 10MB per origin, browser-dependent | Persists indefinitely until explicitly cleared | Synchronous and JavaScript-readable; never store tokens or personal data, since any XSS (cross-site scripting, a malicious script injected into your page) on the page can read it |
sessionStorage | Data scoped to one tab's lifetime, such as a multi-step form's in-progress state | Same ballpark as localStorage | Cleared when the tab/window closes; not shared across tabs | Same XSS exposure as localStorage, but the shorter lifetime somewhat limits the blast radius |
| Cookies | Data the SERVER needs on every request, such as a session ID, since cookies are automatically attached to HTTP requests | About 4KB per cookie, plus a per-domain cookie-count limit | Persists per the Expires/Max-Age attribute the server sets, or session-only | Set HttpOnly (blocks JavaScript access, the main defense for session cookies), Secure (HTTPS only), and SameSite (CSRF, cross-site request forgery, defense); the only one of the four automatically sent on every request, which is also its main risk if scoped too broadly |
| IndexedDB | Larger structured data: an offline product catalog, cached API responses needing complex querying | Hundreds of MB to low GB, quota-based and negotiated with the browser | Persists until explicitly cleared, same as localStorage | Also JavaScript-readable and XSS-exposed; being asynchronous and transactional is a performance/ergonomics property, not a security one |
Choosing between them
The deciding questions are: does the SERVER need to read this automatically, only cookies do that; does it need to survive the tab closing, localStorage and IndexedDB yes, sessionStorage no; and how much data, a few KB fits anywhere, hundreds of KB to MB pushes you toward IndexedDB. Avoid putting an auth token in localStorage/sessionStorage when possible: an HttpOnly cookie is the only one of the four immune to a JavaScript-based XSS read.
Explain semantic versioning (semver) for UI components and a shared component library. Provide concrete examples: (a) a bug fix that should be a patch, (b) a new non-breaking feature that should be a minor bump, and (c) a breaking API change that should be a major version. Describe how you would communicate these different release types to downstream teams and tools you would use for changelogs.
Sample Answer
Direct answer
Semantic versioning gives a component library's version number, MAJOR.MINOR.PATCH, a contract meaning: PATCH is a backwards-compatible bug fix, MINOR is a backwards-compatible addition, and MAJOR is a change that can break a consumer. The point is that a consumer can safely auto-update on patches and minors (^1.4.2 in npm terms) without reading a changelog, and knows a major bump means "stop and read before upgrading."
Structured elaboration
What changes at each level
| Bump | Meaning | Consumer impact | Example trigger |
|---|---|---|---|
| PATCH | Backwards-compatible bug fix | Safe to auto-update, no code changes needed | Fixing a missing aria-label on an icon-only button |
| MINOR | Backwards-compatible addition | Safe to auto-update, new capability is opt-in | Adding a new size="xxs" prop or a new Badge component |
| MAJOR | Breaking change to public API or rendered output | Requires the consumer to read the migration guide and update code | Renaming <Button variant="primary"> to <Button tone="brand"> |
The test for "does this need a major bump" is not "did the code change a lot," it is "does any existing correct usage now behave differently or fail to compile/render." A large refactor that keeps the public API and visuals identical is still a patch.
Communicating each release type
- Changelog: generate it from commit messages using a convention (Conventional Commits:
fix:,feat:,feat!:orBREAKING CHANGE:footer) so the bump level and the changelog entry come from the same source of truth instead of being decided twice. - Release notes for majors: always include a migration guide with before/after code snippets, not just a prose description of what changed.
- Distribution channels: GitHub Releases or an internal package registry page for the human-readable notes, plus an in-repo
CHANGELOG.mdso it is visible in the diff of any PR that bumps the dependency.
Tooling
- Automated bump and changelog:
semantic-releaseorchangesetsread Conventional Commits and produce the version bump, changelog, and tag automatically, removing the "did I mean minor or patch" judgment call from a human at release time. - Breaking-change detection assist: a type-diff check (for example comparing the public
.d.tsAPI surface between versions) catches accidental breaking changes that were not intentionally flagged withfeat!:, which is a common source of "silent major" bugs.
Worked example
A library is at 1.4.2. Three independent changes land:
- (a) Patch, 1.4.2 -> 1.4.3:
Button's icon-only variant is missing anaria-label, so screen readers announce nothing. The fix adds the label. The prop API, markup structure, and visual layout are unchanged for every existing usage, so this is a bug fix with zero consumer action required. - (b) Minor, 1.4.3 -> 1.5.0: A new
Badgecomponent is added, andButtongains an optionalsize="xxs"prop that defaults to the previous size when omitted. Every existing usage ofButtoncompiles and renders identically; only code that opts in to the new prop is affected. This is additive, so it is a minor bump, not a patch, because it is new surface area (even though nothing breaks). - (c) Major, 1.5.0 -> 2.0.0:
Button'svariantprop is renamed totone, and the values change from"primary" | "secondary"to"brand" | "neutral". Any component currently written as<Button variant="primary">now either fails to compile (in TypeScript) or silently renders with no styling (in plain JS, sincevariantis simply ignored). That is a breaking change to every consumer using the old prop, so it is a major bump regardless of how small the code diff looks.
Trade-offs and pitfalls
- Shipping a behavior change "as a patch" because it feels small is the most common semver violation; if any correctly-written existing usage now behaves differently, it is not a patch, no matter how minor it looks to the author.
- Renaming or removing a prop without a deprecation window forces every consumer to upgrade and migrate simultaneously; a senior answer proposes keeping the old prop working (with a console deprecation warning) for at least one major cycle, or shipping a codemod (an automated script that mechanically rewrites consumer code from the old API shape to the new one, so teams don't hand-edit every call site), rather than a hard cutover.
- Auto-generated changelogs from commit messages are only trustworthy if the team actually follows the commit convention; a mislabeled
fix:on a change that is actually breaking produces a version that lies about its own risk, which is worse than a hand-written changelog because consumers trust the automation. - Treating "internal refactor with no API change" as needing any bump at all is unnecessary churn; reserve version bumps for changes visible to a consumer.
Product wants the fastest possible improvement to page load time and gives you four weeks. You're weighing frontend work like lazy-loading and code-splitting against a backend API-aggregation effort. Which would you recommend starting with, and what would change your answer?
Sample Answer
Approach
Trace what actually blocks the page from being usable before picking a side, since page load time is a chain, and lazy-loading (deferring resources until needed) and code-splitting (breaking a large JavaScript bundle into smaller pieces loaded on demand) fix a different part of that chain than API aggregation (combining multiple backend calls into fewer round trips).
Worked estimate (illustrative numbers)
Say a trace shows the page currently loads in 4.5 seconds: about 1.5 seconds comes from a chain of 8 sequential API calls at roughly 180 milliseconds round trip each, about 2 seconds from downloading, parsing, and running a large, unsplit JavaScript bundle, and the remaining second from rendering and images.
Aggregating those 8 calls into 2 combined calls could plausibly cut that 1.5 seconds down to 400 to 500 milliseconds, roughly a 1 second win. Code-splitting the bundle so only what is needed for first paint loads up front could plausibly cut the 2 second JavaScript cost by half or more, roughly another 1 to 1.2 second win. In this profile the two options are close in raw impact, so the tie-breaker becomes speed and risk to build in four weeks. API aggregation usually touches fewer moving parts and can ship incrementally, one endpoint at a time, with fast, measurable wins, while a full code-splitting pass across a codebase, deciding what is route-critical versus deferred, tends to take longer to get right.
What would change the answer
If the trace showed one side clearly dominating, say 3 of the 4.5 seconds coming from the sequential API calls, I would start there since it has the higher ceiling. If most users are on a fast connection but a smaller, high-value segment is on mobile or slow networks where the JavaScript payload dominates their experience, that shifts the priority depending on who "fastest possible" is really meant to serve. And if the backend calls are shared with other teams' services, aggregating them carries wider blast radius and may take longer to get sign-off on than a frontend-only change, which pushes the four-week bet toward frontend first.
After a working meeting, write a concise summary (3-6 sentences) that captures the decision made, who owns each follow-up, the deadlines, and any question that is still open.
Sample Answer
Direct answer
Write a short summary right after the meeting that states the decision made, names an owner and deadline for each follow-up, and flags anything still unresolved, so nobody has to reconstruct what happened from memory a week later.
Structured elaboration
- State the decision first, in one sentence, even if it feels obvious right after the meeting; it stops being obvious within a day or two, especially for people who weren't in the room.
- List action items with an owner and a deadline each, not a bare to-do list; "someone should look into X" is not actionable, "Priya will check the vendor SLA by Thursday" is.
- Name what's still open, explicitly, rather than letting it quietly drop; a one-line "not yet decided: whether we notify customers proactively" prevents someone assuming it was implicitly settled.
- Send it promptly, ideally within the hour, while the details are fresh and before people have moved on to something else and stopped tracking it mentally.
- Keep it short. Three to six sentences is usually enough; a summary that's as long as a transcript won't get read.
Worked example
"Decision: we're moving the schema migration to next Tuesday's low-traffic window instead of doing it live this week. Action items: Priya to update the migration runbook by Monday EOD; Sam to notify the on-call rotation of the new window by Friday. Open question: whether we need a customer-facing heads-up, still deciding, will confirm by Wednesday."
Three sentences, one decision, two owned action items with deadlines, and one explicitly flagged open item.
Trade-offs and pitfalls
- The most common failure is writing a summary that lists what was discussed instead of what was decided; a meeting can generate a page of discussion and one real decision, and the summary should reflect that ratio.
- An action item without a named owner tends to silently not get done; if you can't name an owner in the summary, that's a sign the meeting didn't actually resolve who's responsible.
- Sending it too late (days later) defeats the purpose; by then people have already formed their own, sometimes conflicting, memory of what was agreed.
You notice your team and a neighboring team both think they own the same piece of a shared system, and the overlap is causing duplicated work and confusion about who's responsible for what. How do you sort out the ownership question and keep it from recurring?
Sample Answer
Direct answer
Get both teams in the same room with concrete evidence of the overlap, not each team's assumption about who owns what, agree on a single ownership model for the disputed piece, write it down somewhere both teams will actually find later, and set a lightweight recurring check so the boundary does not quietly drift back into ambiguity.
Structured elaboration
Start with evidence, not opinion
Map the actual overlap: which capability, which parts of the system, which decisions each team has been making independently. A short, concrete inventory, such as "both teams modified this component in the last quarter, for these reasons," turns a "whose job is this" argument into a shared problem to solve.
Choose an ownership model, do not just split the difference
Common options: one team owns it fully and the other is a client of it, ownership is split along a clear seam such as by data domain or by interface, or the piece gets consolidated into a single shared service with one clear owner. Whichever you pick, the test is whether a new engineer joining either team could read the agreement and know who to ask.
Write it down where it will be found
A decision made in a meeting and never documented decays within a sprint. Put the ownership boundary in the same place engineers already look, such as a README, a service catalog, or an API contract doc, not a one-off meeting note.
Set a recurring, lightweight check
A short standing sync between the two teams for boundary-crossing changes, or a simple rule that any change to the shared piece pings both teams, is enough to catch drift early without adding heavy process.
Worked example
Two teams both maintain code that retries failed requests to a downstream service, each having added its own retry and backoff logic independently over time. The overlap surfaces when a production incident review shows both teams' logic firing on the same failure and compounding retry pressure on the downstream service.
The teams map the overlap and find one team's logic lives in a shared client library, while the other's is inline in their own service and duplicates the same behavior. They agree the shared library should be the single source of retry logic, with the other team's inline logic removed and replaced by a call to the library. They write this into the library's README as "owned by Team A, changes to retry behavior require a ping in the shared channel," and add a short section to each team's onboarding doc pointing new engineers at the library first. They also add a lightweight rule: any pull request touching retry or backoff logic in either codebase gets a reviewer from the other team tagged automatically.
Trade-offs and pitfalls
Consolidating too aggressively can overstep a team's actual mandate and create a bottleneck if the new sole owner becomes a blocker for changes the other team needs quickly. Splitting too finely, dividing by an overly granular seam, creates new edge cases at the new boundary instead of removing them.
The common failure mode is not picking the wrong model, it is skipping the documentation and recurring-check steps because the meeting felt like it resolved things. Verbal agreements between the two people in the room do not survive a reorg or a new hire; only a written, discoverable agreement does.
Design an end-to-end approach to serving images in modern formats at the right sizes: when to generate variants, how the right one reaches each browser, how it is cached, and which loading choices protect LCP.
Sample Answer
Direct answer
Generate a fixed ladder of widths in AVIF, WebP and JPEG, eagerly for images that can be the main content of a page and on first request for the long tail. Let the browser choose among them with <picture> and srcset so every variant has its own URL, fingerprint each URL (a hash of the file's content sits in its name, so changed content means a new URL) and cache it for a year, keep the HTML itself revalidating (the browser asks the server whether its stored copy is still current before using it), and protect LCP (Largest Contentful Paint, the moment the largest visible element finishes rendering; web.dev's target is 2.5 seconds) by making the hero a real <img> in the HTML with fetchpriority="high", explicit dimensions and no lazy loading.
When to generate variants
| Image kind | When | Why |
|---|---|---|
| Hero and other above-the-fold images on key pages | At upload or build time | They decide LCP, so they must exist before the first visitor asks |
| Long-tail catalogue images | On first request, then stored | Most variants of most images are never requested, so pre-making them wastes storage and encoding time |
| Never | On every request without storing the result | Pays the encode cost repeatedly |
The ladder is the set of widths you produce. Derive it from the layout, not from guesswork: take the widths your layout gives the image (say 360, 720 and 1200 CSS pixels at three breakpoints) and multiply by the densities you serve (1x and 2x). That gives six pixel widths, and the first candidate in a 480, 960 and 1920 ladder that is at least as wide serves each:
| Slot (CSS px) | Density | Pixels wanted | Ladder width used |
|---|---|---|---|
| 360 | 1x | 360 | 480 |
| 360 | 2x | 720 | 960 |
| 720 | 1x | 720 | 960 |
| 720 | 2x | 1440 | 1920 |
| 1200 | 1x | 1200 | 1920 |
| 1200 | 2x | 2400 | 1920 (the largest candidate; nothing is wide enough) |
Five of the six are covered; only the 2x full-width case falls short, and capping at 1920 is a deliberate choice because the extra bytes for a 2x full-width desktop hero rarely show. Never produce a width larger than the source (upscaling adds bytes, not detail) or larger than twice your biggest slot.
With three widths and three formats a fully pre-generated image is 3 x 3 = 9 files. For a catalogue of 10,000 images, 200 of them heroes: the heroes cost 200 x 9 = 1,800 files up front, and the other 9,800 images cost at most 9,800 x 9 = 88,200 files, created only for variants somebody requests. Everything pre-generated would be 90,000 files (1,800 + 88,200). Those counts are illustrative inputs.
The browser-visible cost of on-demand generation is that the very first request for a variant waits for the encode, so any image that can be a page's LCP element belongs in the eager row.
How the right variant reaches each browser
| Approach | How it works | Cost |
|---|---|---|
Markup: <picture> with type on each <source>, srcset widths and sizes | The browser picks format and width itself; every variant has a distinct URL | You control the HTML; one URL per variant to produce and cache |
Server negotiation (content negotiation: the server picks the format for one URL): the browser sends an Accept request header listing the image types it decodes, and the server returns AVIF, WebP or JPEG | Works for HTML you cannot change (content from a content management system, CSS backgrounds, third-party embeds) | The same URL now returns different bytes, so the browser's own cache must key on Accept: MDN says that including Vary "ensures that responses are separately cached based on the headers listed in the Vary field", used most often "to create a cache key when content negotiation is in use". The response must carry Vary: Accept, and the width must still be encoded in the URL |
What the exchange looks like: Chromium 130, asked for an <img>, sent the request header Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8 (logged by adding console.log(req.headers.accept) to the demo's server below). A negotiating server that sees image/avif in it answers that URL with Content-Type: image/avif and Vary: Accept; a browser whose header lacks image/avif gets the next format on the list at the same URL.
Recommendation: markup first, because it needs no special cache rules and the browser also accounts for screen density. Use Accept negotiation only where the markup is out of your hands. The run at the end shows the markup path choosing the 960 pixel AVIF at 400 CSS pixels and 2x density.
How it is cached
Put a hash of the content in each variant's name, for example /img/hero.9f3a1c-960.avif. A new upload gets a new hash and a new URL, so the old URL's bytes never change and can be cached for a long time. MDN's cache-busting guidance gives this exact pattern: "You can add a long max-age value and immutable because the content will never change." The long max-age is what keeps the variant fresh; immutable adds a promise on top of it.
# /img/* (fingerprinted image variants)
Cache-Control: max-age=31536000, immutable
# /index.html (the page that names the current fingerprints)
Cache-Control: no-cache
31,536,000 seconds is 365 days. immutable tells the browser the response will not change while it is fresh, so a user reload need not send a conditional request for it (MDN: it "avoids those kinds of unnecessary conditional requests to the server"). Browsers differ in whether a reload revalidates subresources at all, so treat immutable as the safe addition and the year-long max-age as the part that does the work. no-cache on the HTML makes the browser revalidate each time (send a conditional request and use the stored copy only if the server confirms it is current), so a page always names the current images (MDN recommends it for the HTML that references hashed assets). The demo at the end sends the first header and confirms that a second page view does not request the image again. In headless Chromium 130 that result comes from the freshness lifetime: the same check still passes with immutable removed, and replacing the header with no-cache gives two requests.
Which loading choices protect LCP
- Make the hero discoverable in the HTML. web.dev lists "The LCP element requires a CSS background image." among the cases where the image is not discoverable from the HTML: the browser learns the URL only after the stylesheet is applied. Use an
<img>in the markup, or a preload. - Set
fetchpriority="high"on that one image (web.dev: "It's a good idea to set fetchpriority="high" on an <img> element if you think it's likely to be your page's LCP element", and more than one or two high-priority images make priority "unhelpful"). - Never lazy-load it. web.dev: "Never lazy-load your LCP image, as that will always lead to unnecessary resource load delay, and will have a negative impact on LCP."
- Give it
widthandheightso layout does not shift when it arrives. - Preload with the same candidate list when the markup cannot be changed, so the preload and the real element pick the same file (the demo checks for a single request).
web.dev's guideline split of LCP time is about 40% time to first byte, under 10% resource load delay, about 40% resource load duration and under 10% element render delay, "guidelines, not strict rules". (Time to first byte is how long the server takes to start answering.) Image work shrinks the two middle parts: delay (steps 1 to 3) and duration (the ladder and formats). For a 2,500 ms target that guideline is 1,000 + at most 250 + 1,000 + at most 250 ms.
Practical per-image optimizations
Five practical optimizations apply to every image:
- Format: AVIF or WebP first, JPEG as the fallback, chosen by the browser.
- Several sizes: the width ladder above, so phones do not download desktop files.
- Lazy loading:
loading="lazy"for images below the fold, never for the LCP image. - Dimensions:
widthandheight(oraspect-ratio) on every image. - Compression quality: one quality setting per format judged by eye on real images, applied by the pipeline rather than by each editor.
Verified: one request, right variant, cached
The demo serves a fingerprinted AVIF ladder to an image slot 400 CSS pixels wide (sizes="50vw" in an 800 pixel viewport) at 2x density, with a preload (a <link rel="preload"> that makes the browser fetch the image early) that carries the same imagesrcset and imagesizes (the preload's copies of srcset and sizes) as the <source>, then reloads the page. It prints every image request the server saw. A preload that leaves out imagesizes assumes 100vw, picks a different file and the check fails. The check proves that the preload and the element agree on one file; it does not measure how much earlier the preload starts the download.
// preload-demo.mjs
import http from 'node:http';
import { chromium } from 'playwright';
const BREAK = process.env.BREAK || ''; // 'nosizes' drops imagesizes from the preload, so it assumes 100vw and fetches a different file
const PIXEL = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==', 'base64');
const SET = '/img/hero.9f3a1c-480.avif 480w, /img/hero.9f3a1c-960.avif 960w, /img/hero.9f3a1c-1920.avif 1920w';
const PRELOAD = BREAK === 'nosizes'
? `<link rel="preload" as="image" type="image/avif" imagesrcset="${SET}" fetchpriority="high">`
: `<link rel="preload" as="image" type="image/avif" imagesrcset="${SET}" imagesizes="50vw" fetchpriority="high">`;
const PAGE = `<!doctype html><head>${PRELOAD}</head><body style="margin:0"><picture>
<source type="image/avif" srcset="${SET}" sizes="50vw">
<img src="/img/hero.9f3a1c-960.jpg" width="1920" height="1080" alt="" fetchpriority="high" style="width:50%;height:auto">
</picture></body>`;
const hits = [];
const server = http.createServer((req, res) => {
if (req.url === '/') { res.setHeader('content-type', 'text/html'); return res.end(PAGE); }
hits.push(req.url);
res.setHeader('content-type', req.url.endsWith('.avif') ? 'image/avif' : 'image/jpeg');
res.setHeader('cache-control', 'max-age=31536000, immutable'); // what a fingerprinted variant should send
res.end(PIXEL);
}).listen(0);
const browser = await chromium.launch();
const ctx = await browser.newContext({ viewport: { width: 800, height: 800 }, deviceScaleFactor: 2 });
const page = await ctx.newPage();
await page.goto(`http://localhost:${server.address().port}/`);
await page.waitForFunction(() => document.querySelector('img').complete);
await page.waitForTimeout(300);
await page.reload(); // second view: the immutable variant must not be requested again
await page.waitForTimeout(500);
console.log('requests for images:', JSON.stringify(hits));
const ok = hits.length === 1 && hits[0] === '/img/hero.9f3a1c-960.avif';
console.log(`${ok ? 'PASS' : 'FAIL'}: one request in total across two page views, for the 960 px AVIF variant`);
await browser.close(); server.close(); process.exit(ok ? 0 : 1);
requests for images: ["/img/hero.9f3a1c-960.avif"]
PASS: one request in total across two page views, for the 960 px AVIF variant
Reading it: the server logs one image request only. The page view chose the 960 px file (50vw of 800 is 400 CSS px, x 2 = 800 pixels wanted, so 960), the preload and the <picture> agreed on it so it was fetched once, and the reload was served from the browser's cache because the variant was still fresh under its max-age=31536000. With BREAK=nosizes the preload drops imagesizes, assumes 100vw (1600 pixels wanted at 2x) and fetches the 1920 px file as well, so the log shows two requests (["/img/hero.9f3a1c-1920.avif","/img/hero.9f3a1c-960.avif"]) and the check prints FAIL with exit code 1.
Trade-offs and pitfalls
- More variants means more storage and more URLs to keep consistent. Cap the ladder and formats; add a width only when RUM (real user monitoring: measurements taken in actual visitors' browsers) shows a population that downloads a wastefully large file.
- A wrong
sizesvalue defeats the ladder: the browser trusts it when picking a width. - Fingerprints need a build or upload step that rewrites every reference; a missing rewrite serves stale images for a year.
- Always provide the JPEG: browsers that cannot decode AVIF or WebP get it.
Running the code
mkdir demo && cd demo && echo '{"type":"module"}' > package.json
npm i playwright@1.48.2 # save the listing above as preload-demo.mjs, then:
docker run --rm -v "$PWD":/w -w /w mcr.microsoft.com/playwright:v1.48.2-jammy node preload-demo.mjs
# add -e BREAK=nosizes before the image name to drop imagesizes from the preload: it fetches the 1920 px file as well, FAIL line, exit code 1
The printed output comes from the mcr.microsoft.com/playwright:v1.48.2-jammy image (headless Chromium 130.0.6723.31, aarch64 Linux container).
You're just days away from a scheduled release when a serious problem surfaces (a performance regression, or the shipped UI deviating from the approved design in several places) and the business wants to keep the release date. Describe step by step how you would take ownership of diagnosing and resolving it within the shrinking window, including any quick mitigations, the tools you'd use, and how you'd communicate with stakeholders while minimizing risk to users.
Sample Answer
Direct answer
Triage fast to find the smallest change that removes the actual user-facing harm, use the shrinking window as a reason to scope down rather than attempt a full root-cause rebuild days before release, and tell stakeholders the real state and the fallback option early rather than quietly hoping the fix lands cleanly.
Structured elaboration
Diagnose fast and scoped: for a performance regression, use a profiler (an in-engine frame-time profiler for a game, or browser devtools for a web frontend) to find the specific function or asset actually causing the slowdown rather than guessing. For a UI deviating from the approved design, do a direct side-by-side comparison against the design file to produce a specific list of deviations rather than a vague sense that "it looks off."
Quick mitigations: for a performance issue, a targeted fix (reducing a specific effect's cost, lazy-loading an asset, capping a repeated computation) beats a broad refactor under this kind of time pressure. For a design deviation, fix whatever is visually significant or brand-critical first, and explicitly defer pixel-level polish that does not affect usability to a fast-follow.
Tools: a profiler to find the real hotspot, a visual diff or side-by-side screenshot comparison against the design spec, and a feature flag or config toggle that can instantly revert the change if the fix itself turns out risky right before release.
Communicate with stakeholders: give them the specific finding, not just "there's an issue," the fix plan, and a fallback (ship with the change flagged off, or revert to the last known-good build) if the fix is not verified in time, so the release date does not depend entirely on one fix landing perfectly.
Worked example
Four days from a mobile game's release, frame-rate profiling shows a specific particle effect added the previous sprint is causing a drop from 60 to 40 frames per second in one busy scene. Day one: profile and confirm it is that one effect, not a broader issue. Day two: reduce the effect's particle count and cap it behind a quality setting rather than removing it entirely, and verify frame rate recovers to around 58 to 60 in the same scene. Day three: regression-test the fix across the other scenes using the same effect. Day four, release day: ship with the capped effect, communicated to the team as a deliberate scope trim rather than a mystery late fix.
Trade-offs and pitfalls
The clearest pitfall is attempting a deep architectural fix for either kind of problem this close to release instead of the smallest safe change available. A second is not telling stakeholders until the last possible moment, which removes their ability to choose a fallback if the fix risks slipping. The same discipline applies to the design-deviation version of this problem: fix what actually matters to users and the brand, and explicitly log, rather than silently drop, the cosmetic deviations for a fast-follow patch instead of trying to close every gap before the date.
During sprint planning you encounter several incomplete user stories. As the engineer, which questions do you ask in grooming, when do you recommend a spike, and what deliverables should a spike produce so the story can be estimated and scheduled?
Sample Answer
Grooming, spike triggers, and spike deliverables are three separate decisions, and treating a spike as a substitute for a clarifying question that already has a known answer is the single most common mistake here.
Questions to ask in grooming. "What's the acceptance criteria, how will we know this is actually done?" surfaces a missing definition of done. "Is there a design or mock, or an existing pattern in the codebase we're extending?" surfaces a missing design. "Does this touch a system we don't fully understand yet, a third-party API, a legacy module, an unfamiliar data model?" surfaces a real technical unknown. "What's explicitly out of scope?" prevents scope from silently creeping in mid-build. "Does this depend on another team or story that isn't finished yet?" surfaces a sequencing risk before it becomes a blocked sprint.
When to recommend a spike, not just more grooming. A spike is warranted when the story can't be estimated within a reasonable confidence band because of a genuine unknown, specifically: the team's estimates disagree by an order of magnitude, for example someone says 2 days and someone says 3 weeks, or the story depends on an external system whose real behavior the team hasn't verified firsthand, or it requires touching unfamiliar legacy code with no test coverage. If none of those apply and the team simply hasn't discussed it yet, that's a grooming gap you close with a clarifying question to the PM or stakeholder who already knows the answer, not a spike; reaching for a spike here just delays getting an answer that was already available.
What a spike needs to produce for the story to become estimable and schedulable. A written answer to the specific unknown that triggered the spike, not a general write-up of everything explored. A rough order-of-magnitude estimate for the real story, with a stated confidence level. A list of any new sub-tasks or dependencies the spike uncovered. An explicit note that any code produced is throwaway, not production-ready, flagged as such so nobody accidentally ships it hardened but unreviewed. And if the spike itself runs out of its timebox without an answer, a predefined response: either re-timebox once with a narrower, more specific question, or escalate to the PM that the story likely needs to be broken down further rather than estimated as one unit.
Worked example. A story reads "add support for syncing calendar events from a partner's calendar API." Grooming questions surface that nobody on the team has used this particular partner API before, and its rate-limit and webhook behavior under a bulk sync is undocumented, a genuine unknown, with the team's estimate spread running from 3 days to 3 weeks. That spread is the trigger to recommend a 2-day spike. The spike's deliverable: confirmation that the API supports webhooks rather than requiring polling, a documented rate limit of 500 requests per hour, a revised estimate of 5 to 7 days at medium confidence, and a newly surfaced dependency, a webhook receiver endpoint the story hadn't originally scoped.
Your growth has been slower than you expected, even though you're performing well. How do you handle that gap between your ambition and your actual pace, without either giving up on the goal or coming across as entitled?
Sample Answer
Direct answer
The move is to separate diagnosis from reaction: before assuming the gap is unfair (inconsistent criteria, an indifferent manager) or purely a personal failing, do an honest, evidence-based audit of your own case first, then act on what you actually find rather than on the story you started with. That self-diagnosis is what keeps the response grounded instead of either resigned or entitled.
Structured elaboration
- Name the trigger honestly, whatever it actually is: no advancement across two review cycles, criteria that seem to shift depending on who's managing you, or just a general sense of plateauing. Treat it as a starting observation, not a verdict.
- Diagnose before reacting. Audit your own evidence (scope actually carried, outcomes attributable to you, feedback received) against whatever the organization's stated or informal bar is. Separately, note if the bar itself looks inconsistent or unclear across managers, that's useful information, not an excuse to skip the audit.
- Have the direct conversation. Bring your honest self-assessment to your manager and ask explicitly what's missing, rather than silently accumulating resentment or silently giving up on the goal.
- Decide on a bounded response. Set a real timeframe to see change, and know your own answer for what you'd do if nothing changes, without turning that into an ultimatum in the room.
Worked example
"At one point I'd gone through two review cycles without the scope change I was expecting, and it would have been easy to decide either that the process was broken or that I just wasn't good enough. Instead I sat down and mapped what I could actually point to: had I taken on next-level work, could I show outcomes that were mine, had anyone besides me confirmed it. Some of it held up, some didn't, there was a specific kind of cross-team decision I'd been avoiding making solo. I brought that honest picture to my manager instead of a complaint, asked directly what was missing from their side, and used what came back to set a concrete plan with a real check-in date rather than just waiting quietly for the next cycle."
Trade-offs & pitfalls
- Skipping the self-diagnosis and going straight to a conversation framed as a grievance is the entitled failure mode interviewers are listening for.
- Quietly disengaging or lowering your own ambition instead of raising the issue is the opposite failure mode, and just as costly.
- Blaming inconsistent criteria across managers without also checking your own evidence dodges the harder, more useful half of the question.
- Setting no real timeframe for reassessment lets "wait and see" drift indefinitely. A grounded answer names when you'd revisit the decision.
Why do keys matter when rendering lists in React? Given the following code, explain potential bugs and show an improved approach: {items.map((item, idx) => <ListItem key={idx} data={item} />)}. Describe how React's reconciliation uses keys and why using stable unique ids is important when items are added, removed, or reordered.
Sample Answer
Direct answer
key is how React matches an item in this render's list to the same item in the previous render. A component instance is one live copy of a component that React is keeping on screen, with its own state and DOM nodes; two rows rendered from the same Row function are two instances. To mount an instance is to create it for the first time, and to unmount it is to destroy it and discard its state. A render is different: it is React calling the component function again on an existing instance. With key={idx} the identity is the item's position, so when items are added, removed or reordered, React hands each component instance the data of whatever item now sits at its old position. Props update, but anything that lives in the instance (state, uncontrolled input text, focus, an in-flight effect) stays at the position and ends up attached to the wrong item. The fix is a key derived from the data: key={item.id}, a stable unique value that travels with the item.
How reconciliation uses the key
Reconciliation is React's comparison of the previous element tree with the new one to decide what to update. For a list of siblings, React pairs old and new children by key (falling back to position when no key is given). A pair with the same key and type keeps its component instance and receives new props. A new key is mounted; a missing key is unmounted. React's documentation puts it this way: "Keys tell React which array item each component corresponds to, so that it can match them up later," and "Index as a key often leads to subtle and confusing bugs." It also notes that omitting the key makes React use the index anyway.
Reproducing the bug
The question's snippet is items.map((item, idx) => <ListItem key={idx} data={item} />). The program below renders rows of three items, each with two kinds of per-row state: a "star" toggle held in useState and a note typed into an uncontrolled <input> (one whose text lives in the DOM node, not in React state). It stars item A and types a note on it, then prepends a new item Z, and runs the scenario under three key strategies. Row is wrapped in memo (React.memo, which skips calling the function again when every prop is unchanged) so the render counts show real work (a function that React ran again).
import { JSDOM } from 'jsdom';
import assert from 'node:assert/strict';
const dom = new JSDOM('<!doctype html><body></body>', { url: 'http://localhost' });
globalThis.window = dom.window; globalThis.document = dom.window.document;
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
const React = (await import('react')).default;
const { render, act, cleanup, fireEvent } = await import('@testing-library/react');
const { useState, useEffect, memo } = React;
const h = React.createElement;
let stats;
const Row = memo(function Row({ item }) {
stats.renders++;
const [starred, setStarred] = useState(false); // state kept by React, tied to the row's identity
useEffect(() => { stats.mounts++; }, []);
return h('li', { 'data-name': item.name },
item.name, ' ',
h('button', { onClick: () => setStarred((s) => !s) }, starred ? 'starred' : 'star'),
h('input', { 'aria-label': 'note ' + item.name })); // uncontrolled: the DOM node holds the typed text
});
let randomCounter = 0;
const keyFor = { index: (item, i) => i, id: (item) => item.id, random: () => 'k' + randomCounter++ };
if (process.env.BREAK) keyFor.id = (item, i) => i; // BREAK=1 removes the technique: ids no longer used
function List({ items, mode }) {
return h('ul', null, items.map((item, i) => h(Row, { key: keyFor[mode](item, i), item })));
}
function scenario(mode) {
stats = { renders: 0, mounts: 0 };
const A = { id: 'a', name: 'A' }, B = { id: 'b', name: 'B' }, C = { id: 'c', name: 'C' }, Z = { id: 'z', name: 'Z' };
const { container, rerender } = render(h(List, { items: [A, B, C], mode }));
const li = (n) => container.querySelector(`li[data-name="${n}"]`);
fireEvent.click(li('A').querySelector('button')); // star A
fireEvent.change(li('A').querySelector('input'), { target: { value: 'note for A' } });
stats.renders = 0; stats.mounts = 0;
rerender(h(List, { items: [Z, A, B, C], mode })); // prepend Z
const rows = [...container.querySelectorAll('li')].map((l) => ({
name: l.dataset.name, star: l.querySelector('button').textContent, note: l.querySelector('input').value }));
cleanup();
return { rows, renders: stats.renders, mounts: stats.mounts };
}
for (const mode of ['index', 'id', 'random']) {
const r = scenario(mode);
console.log(`key=${mode}: renders after prepend=${r.renders}, mounts after prepend=${r.mounts}`);
for (const row of r.rows) console.log(` ${row.name} ${row.star} note="${row.note}"`);
if (mode === 'index') {
assert.deepEqual(r.rows[0], { name: 'Z', star: 'starred', note: 'note for A' }); // A's state landed on Z
assert.equal(r.rows[1].star, 'star');
assert.equal(r.mounts, 1); assert.equal(r.renders, 4);
} else if (mode === 'id') {
assert.deepEqual(r.rows.find((x) => x.name === 'A'), { name: 'A', star: 'starred', note: 'note for A' });
assert.deepEqual(r.rows[0], { name: 'Z', star: 'star', note: '' });
assert.equal(r.mounts, 1); assert.equal(r.renders, 1);
} else {
assert.equal(r.mounts, 4); assert.equal(r.rows.find((x) => x.name === 'A').note, '');
}
}
console.log('all assertions passed');
Run in a node:22 container it prints:
key=index: renders after prepend=4, mounts after prepend=1
Z starred note="note for A"
A star note=""
B star note=""
C star note=""
key=id: renders after prepend=1, mounts after prepend=1
Z star note=""
A starred note="note for A"
B star note=""
C star note=""
key=random: renders after prepend=4, mounts after prepend=4
Z star note=""
A star note=""
B star note=""
C star note=""
all assertions passed
Reading the three runs:
key=index: the star and the note typed for A are now shown on Z, and A shows as unstarred with an empty note. Position 0 kept its instance (with its state) while the data changed under it. This is the bug, and it is worse for editable rows than for static text. All 4 rows rendered, because every position received a different item, and only 1 mounted (the new last position).key=id: A keeps its star and note, Z is a fresh row, and only 1 row rendered at all, because the three existing rows received equal props andmemoskipped them. Stable keys are also what make the cheap path possible.key=random(the "generate keys on the fly" mistake): all 4 rows were destroyed and recreated, A lost both pieces of state, and the work is the same as a full remount of the list. React's documentation warns that this "will cause keys to never match up between renders, leading to all your components and DOM being recreated every time."
With BREAK=1 the id strategy is replaced by the index. Run in the same container, the id run then repeats the index result and the first id assertion fails, so the demo proves the key is what carries the state:
key=id: renders after prepend=4, mounts after prepend=1
Z starred note="note for A"
A star note=""
B star note=""
C star note=""
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected
{
name: 'A',
+ note: '',
+ star: 'star'
- note: 'note for A',
- star: 'starred'
}
The A row shows star with an empty note instead of starred with its note.
Choosing a good key
- Use a stable, unique id from the data: a database primary key, or an id assigned when the item is created (for example a counter or
crypto.randomUUID()called when the item is added to state). Never assign it during render, since a value computed in render changes on every render, which is therandomcase above. - Keys need to be unique only among siblings, not globally.
- Index keys are acceptable only for a list that is static and never reorders, filters or inserts, such as the lines of a poem (the same React page makes this exception).
- If you filter or sort, the same id still identifies the same item, so inputs, focus and animations follow the item.
- Changing a key deliberately uses the same mechanism the
randomrun showed: a new key means a new instance with fresh state, so<Form key={userId} />starts from empty state wheneveruserIdchanges.
Trade-offs and pitfalls
- The index bug is silent when rows hold no state: a read-only list "works" with index keys, which is why it survives code review. It appears once a row gains an input, a checkbox, focus, a CSS animation or a fetch in an effect.
- An id is only a good key if it is truly unique and stable. Using a field like
namethat users can edit or that can collide produces the same class of bug. - This run is a single scenario (one prepend on three items). It shows the mechanism, not performance numbers.
Running the code
npm i react@18.3.1 react-dom@18.3.1 @testing-library/react@16.0.1 @testing-library/dom@10.4.0 jsdom@25.0.1
node keys.mjs # prints the three runs and "all assertions passed"
BREAK=1 node keys.mjs # id strategy replaced by index: assertion fails
Save the listing as keys.mjs beside a package.json containing { "type": "module" }. Tested on node:22 (v22.23).
Want to create your own tailored preparation guide using our deep research?
Get Started for FreeInterview-Ready Courses
Visual-first, interactive, structured learning paths
Browse Frontend Developer jobs
AI-enriched listings across hundreds of company career pages
Explore Jobs