Machine Coding & Low Level Design
Machine Coding General Approach
In machine coding rounds (typically 45–60 minutes), follow this structured step-by-step workflow:
- Clarify Requirements: Confirm features, constraints, responsive expectations, and edge cases before typing code.
- Define API & Props: Design clean, extensible component interfaces.
- Build Minimal Working Version: Get functional core state running first.
- Handle Edge Cases: Empty states, loading spinners, error handling, rapid user interactions.
- Accessibility (a11y): Keyboard navigation, ARIA roles, focus management.
- Explain Trade-offs: Discuss state placement, performance, and scalability options.
Essential Machine Coding Components
1. Counter
- Core Concept: Increment, decrement, reset.
- Key Details: Use functional updater form
setCount(c => c + 1)to prevent stale closure bugs. Memoize callbacks withuseCallbackonly when passed to optimized memoized children.
2. Todo List + LocalStorage
- Core Concept: Persistent state management with browser storage.
- Key Details:
- Lazy initialize state from
localStorage:useState(() => JSON.parse(localStorage.getItem('todos')) || []). - Wrap storage reads/writes in
try/catchto gracefully handle disabled storage or quota exceeded errors.
- Lazy initialize state from
3. Search Input with Debouncing
- Core Concept: Throttling/debouncing rapid input changes to prevent API flooding.
- Key Details:
- Implement custom
useDebounce(value, delay)hook. - Use
AbortControllerto cancel in-flight stale requests on input changes to prevent race conditions.
- Implement custom
4. Modal Component
- Core Concept: Reusable overlay dialog rendered outside normal DOM layout.
- Key Details:
- Use
ReactDOM.createPortalto render intodocument.body. - Focus trap inside the modal while open.
- Close on
Escapekeypress and backdrop overlay clicks. - Set
role="dialog",aria-modal="true", and restore focus to trigger button upon closing.
- Use
5. Infinite Scroll List
- Core Concept: Lazy data fetching triggered as user scrolls near bottom.
- Key Details:
- Use
IntersectionObserverattached to a sentinel element at the bottom of the list. - Maintain
isLoadingandhasMoreflags to guard against duplicate requests.
- Use
6. Form with Real-time Validation
- Core Concept: Controlled/uncontrolled form handling with field validation.
- Key Details:
- Validate fields on blur and submit initially; switch to real-time validation on change once an error has occurred.
- Associate error messages using
aria-describedbyfor accessibility.
7. Theme Switcher (Dark / Light Mode)
- Core Concept: Application-wide theme context with persistence and zero flash on load.
- Key Details:
- Use Context API combined with CSS custom properties (
var(--bg-color)). - Respect
prefers-color-schememedia query. - Inject an inline script in the HTML
<head>to set initial theme attribute before React hydration to prevent flash of unstyled theme (FOUT).
- Use Context API combined with CSS custom properties (
8. Auto-complete / Typeahead
- Core Concept: Suggestion list filtering with keyboard navigation.
- Key Details:
- Debounce search inputs and cache API query results.
- Support
ArrowUp,ArrowDown,Enter, andEscapekey navigation. - Set
role="combobox",aria-expanded, andaria-activedescendant.
9. 5-Star Rating Component (Amazon Style)
- Core Concept: Interactive rating control with hover preview and half-star support.
- Key Details:
- Track
ratingvalue state andhoverRatinginteraction state. - Render half-stars using CSS
clip-path: inset(0 50% 0 0)or SVG masks. - Support keyboard adjustment (
ArrowLeft/ArrowRight). - Use
role="radiogroup"orrole="slider"witharia-valuenow.
- Track