ReviseAlgo Logo

JavaScript Interview Prep

Machine Coding Patterns — JavaScript

Master frontend machine coding interview rounds with essential patterns: Toast Notifications, Accordion, Virtualized List, Auto-complete Search, and Star Rating.

Machine Coding Patterns — JavaScript

Machine Coding is a hands-on technical interview format used by top tech companies (Uber, Atlassian, Flipkart, Swiggy, Razorpay) where candidates are asked to build a fully functional, self-contained UI feature or component in vanilla JavaScript/DOM within 60–90 minutes.

1. Core Principles of Machine Coding Success

1. Clean Separation of Concerns: Keep business/state logic decoupled from DOM rendering. 2. Event Delegation: Attach single listeners to parent containers rather than binding handlers to every dynamic child element. 3. Accessibility (a11y): Use ARIA attributes (aria-expanded, role="alert", keyboard accessibility). 4. Cleanup & Memory Safety: Always clean up event listeners and timers (clearTimeout, clearInterval) to prevent memory leaks.

2. Pattern 1: Toast Notification System

Requirement

Build a global notification toast queue that stacks alerts, supports auto-dismissal timers, and provides custom types (success, error, info).

3. Pattern 2: Auto-Complete Search Bar (Debounced & Cached)

Requirement

Implement an auto-complete input with API debouncing, response caching, keyboard navigation (Arrow Up/Down, Enter), and highlight matching text.

4. Pattern 3: Star Rating Component (Interactive & Accessible)

5. Interactive Code Playground

Test the dynamic implementation of the Star Rating Component below:

6. Machine Coding Checklist for Interviews

  • [x] Clarify Requirements: Single select vs multi select, auto-dismiss timeouts, keyboard navigation.
  • [x] Event Delegation: Avoid querySelectorAll('.item').forEach(el => el.addEventListener(...)).
  • [x] Debounce & Throttle: Apply when handling scroll, resize, or keyup events.
  • [x] Memory Management: Remove timeouts and listeners on unmount.
  • [x] Accessibility: Include aria-expanded, aria-label, and tabindex="0".
  • [x] Edge Cases: Empty lists, fast typing, network errors, long text overflow.
  • 7. Summary Matrix

    PatternPrimary Key ChallengeRecommended Strategy
    Toast NotificationsStacking animations & timer cleanupClosure timers + CSS transition events
    Auto-CompleteRace conditions & server overloadDebouncing + Map cache + AbortController
    Star RatingState sync (hover vs selected rating)State-driven re-render via single event listener
    AccordionCollapsible animations & a11yCSS grid grid-template-rows: 0fr \rightarrow 1fr
    Infinite ScrollHigh DOM node memory footprintIntersectionObserver with pagination guard