Back

Card Deck I

easy

Streak

0 days

Progress

0%

Submitted

0

Card Deck I

React30 mineasyFreeNew

Prompt

Card Deck I introduces the fundamental card flip interaction. A user clicks a card and it flips 180° to reveal its back face. This tests CSS 3D transforms, backface-visibility, and controlled React state. It is the foundation for flashcard and memory-game UIs.

Build a single FlipCard component that shows a front face and a back face. On click, it animates a smooth 3D flip along the Y-axis. The card must stay flipped until clicked again (toggle behaviour).

The CSS challenge is that both faces must occupy the same space and only one is visible at a time. The trick is backface-visibility: hidden plus rotating the back face 180° initially so it starts hidden.

Requirements

  • →Smooth 3D flip animation on click
  • →Front and back faces as separate elements
  • →Toggle: click to flip, click again to unflip
  • →Uses CSS transform-style: preserve-3d
  • →No JS animation — CSS transition only
  • →Card dimensions configurable via props
Example
Loading preview...
For the best coding experience, we recommend using a desktop device.
Preparing Sandbox...
Premium interview report

What interviewers score in this build

Use this before reading the code. It tells you what to say, what to test, and where machine-coding candidates usually lose points.

Interview signals

  • Three-layer structure: Perspective wrapper → preserve-3d flipper → two absolute faces
  • backface-visibility: hidden: Applied to both faces to prevent bleed-through
  • Back face pre-rotated: Back face starts at rotateY(180deg)
  • CSS-only animation: Transition on the flipper, no JS animation

Time checkpoints

  1. 1

    0–3 min: Explain three-layer structure and why preserve-3d is needed

  2. 2

    3–8 min: Build HTML structure: scene → card → front/back

  3. 3

    8–13 min: Apply backface-visibility and pre-rotate back face

  4. 4

    13–17 min: Add toggle state and transition

Edge-case checklist

Empty data and first-load state
Slow network, failed request, and retry path
Keyboard navigation and focus movement
Large input size, re-render pressure, and cleanup

Common mistakes

  • Starting with JSX before naming state and events.
  • Ignoring accessibility until the final minute.
  • Over-building abstractions instead of finishing the required behavior.
  • Failing to narrate trade-offs while coding.
SolutionRead-only · Live Preview

Technical Explanation

Why This Question Exists

Card Flip tests a specific CSS 3D transform technique that trips up most candidates. The immediate instinct is to toggle between two divs using conditional rendering, but that produces no animation. The correct solution uses CSS transform-style: preserve-3d and backface-visibility: hidden — two properties that most developers have heard of but never truly understood.

The Wrong Approach: Conditional Render

// ❌ WRONG — no animation, just swap
{isFlipped ? <div>Back</div> : <div>Front</div>}
// Even adding a transition here doesn't help — you're toggling different elements

The Three-Layer Structure

/* Layer 1: Perspective context — NEVER rotates itself */
.scene {
  perspective: 800px;  /* distance of viewer — larger = more subtle 3D */
  width: 220px;
  height: 140px;
}

/* Layer 2: The card — this is what ROTATES */
.card {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;  /* children live in 3D space */
  transition: transform 0.6s;
}
.card.flipped {
  transform: rotateY(180deg);
}

/* Layer 3: Both faces — occupy the SAME space */
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;  /* hide when rotated > 90deg */
}
.back {
  transform: rotateY(180deg);  /* starts facing away — hidden initially */
}
ℹ Interview Tip

Say: "The three layers are: perspective wrapper (sets the 3D viewing distance and never moves), the flipper (rotates 180deg on click, has transform-style: preserve-3d so children exist in 3D space), and the two faces (absolute-positioned on top of each other, backface-visibility: hidden so only the forward-facing one is visible at any time)."

Why backface-visibility: hidden is required

/* Without backface-visibility: hidden: */
/* At 0deg: front visible, BACK ALSO VISIBLE (mirrored through) */
/* At 90deg: edge-on, neither visible */
/* At 180deg: back visible, FRONT ALSO VISIBLE (mirrored through) */

/* With backface-visibility: hidden: */
/* At 0deg: front visible only */
/* At 90deg: nothing visible (both hidden) */
/* At 180deg: back visible only — correct! */

This property hides an element when its backface (the 180°-rotated side) is facing the viewer. Without it, both faces would bleed through each other and you'd see a confusing overlap.

Why preserve-3d is required

/* Without transform-style: preserve-3d on .card: */
/* The browser flattens children into 2D before compositing */
/* Both faces are flat images that cross-fade, not 3D objects */

/* With transform-style: preserve-3d: */
/* Children (front, back) are positioned in the same 3D space */
/* The rotation of .card rotates both children in that 3D space */

Without preserve-3d, the browser composites the children as flat 2D textures before applying the parent's transform. The flip looks like a flat box rotating, not a card with two sides.

⚠ Common Pitfall: Adding perspective to the card itself

If you add perspective to .card instead of a parent wrapper, the perspective origin moves with the card during rotation — causing a disorienting "fish-eye" effect. Perspective must be set on a non-rotating ancestor.

Interview Criteria

Three-layer structure

Perspective wrapper → preserve-3d flipper → two absolute faces

backface-visibility: hidden

Applied to both faces to prevent bleed-through

Back face pre-rotated

Back face starts at rotateY(180deg)

CSS-only animation

Transition on the flipper, no JS animation

Toggle state

Single boolean isFlipped drives the transform

Time Checkpoints

0–3 min

0–3 min: Explain three-layer structure and why preserve-3d is needed

3–8 min

3–8 min: Build HTML structure: scene → card → front/back

8–13 min

8–13 min: Apply backface-visibility and pre-rotate back face

13–17 min

13–17 min: Add toggle state and transition

17–21 min

17–21 min: Test flip looks correct from both sides

21–25 min

21–25 min: Make dimensions configurable via props

Streak

0 days

Last active: Sign in to track

Progress

0%

0/0 solved

Submitted

0

Solutions pushed to review history.