Muscle Memory Drills
These are the patterns that should become automatic. Practice typing them from memory until you can hit the time targets. Each drill includes an interactive code editor with real test execution - your code runs against actual tests to verify correctness.
How to Practice
Read
Read the prompt and understand what you need to build
Type
Type your solution in the editor - timer starts on first keystroke
Run
Click 'Run Tests' to execute your code against real tests
Repeat
Use hints if stuck, then reset and try again faster
FastAPI GET Endpoint
Create a GET endpoint with path parameters that returns JSON
Create a FastAPI GET endpoint at "/users/{user_id}" that:
1. Accepts a user_id as a path parameter (integer)
2. Returns a JSON object with: {"user_id": <the id>, "name": "User <id>", "active": true}
Example: GET /users/42 should return {"user_id": 42, "name": "User 42", "active": true}FastAPI POST with Pydantic
Create a POST endpoint with Pydantic model validation
Create a FastAPI POST endpoint at "/items" that:
1. Accepts a JSON body with: name (string), price (float), quantity (int, optional, default 1)
2. Uses a Pydantic model called "Item" for validation
3. Returns the item with an added "total" field (price * quantity)
Example: POST {"name": "Widget", "price": 9.99, "quantity": 3}
Returns: {"name": "Widget", "price": 9.99, "quantity": 3, "total": 29.97}SQLAlchemy Model + CRUD
Create a Task model with create and get operations
Create a SQLAlchemy model and CRUD operations: 1. Define a "Task" model with columns: - id: Integer, primary key, autoincrement - title: String(100), not nullable - completed: Boolean, default False 2. Create functions: - create_task(db: Session, title: str) -> Task - get_task(db: Session, task_id: int) -> Task | None The database setup is provided. Focus on the model and CRUD functions.
React Fetch + Display
Fetch data with useEffect and display as a list
Create a React component "UserList" that:
1. Uses useState to store an array of users
2. Uses useEffect to fetch from "/api/users" on mount
3. Displays users in a <ul> with each user's name in an <li>
4. Each <li> should have key={user.id}
Assume the API returns: [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]React Form Submission
Controlled input with POST request and state update
Create a React component "AddTodo" that:
1. Has a controlled text input for the todo title
2. Has a submit button
3. On submit: POST to "/api/todos" with { title: inputValue }
4. After successful POST, add the returned todo to a todos array in state
5. Display the todos list below the form
Assume POST returns the created todo: { id: 1, title: "...", completed: false }Loading + Error States
Implement the three states pattern: loading, error, success
Create a React component "DataFetcher" that: 1. Has three states: loading (boolean), error (string | null), data (any | null) 2. Fetches from "/api/data" on mount 3. Shows "Loading..." while fetching 4. Shows error message if fetch fails 5. Shows the data (as JSON string) when successful Handle the fetch error with try/catch and set appropriate states.
CORS Setup
Configure FastAPI CORS middleware
Configure CORS middleware for a FastAPI app:
1. Import CORSMiddleware from fastapi.middleware.cors
2. Add middleware with:
- allow_origins: ["http://localhost:3000"]
- allow_credentials: True
- allow_methods: ["*"]
- allow_headers: ["*"]
3. Create a test endpoint GET /health that returns {"status": "ok"}useEffect Cleanup
Implement setInterval with proper cleanup on unmount
Create a React component "Timer" that: 1. Uses useState to track elapsed seconds (starts at 0) 2. Uses useEffect with setInterval to increment every 1000ms 3. Returns a cleanup function that calls clearInterval 4. Displays: "Elapsed: X seconds" This prevents memory leaks when the component unmounts.
Optimistic UI Update
Update UI immediately with rollback on failure
Create a React component "LikeButton" that:
1. Shows current like count from props.initialCount
2. On click: immediately increment the count (optimistic update)
3. Send POST to "/api/like"
4. If POST fails: roll back to previous count
5. Show the count in a button: "Like ({count})"
The optimistic pattern: save old state, update UI, then rollback if API fails.WebSocket Echo Server
Create a FastAPI WebSocket endpoint that echoes messages
Create a FastAPI WebSocket endpoint at "/ws" that: 1. Accepts WebSocket connections 2. Sends a welcome message: "Connected!" 3. Continuously receives messages and echoes them back with "Echo: " prefix 4. Handles disconnection gracefully Example: Client sends "hello" -> Server responds "Echo: hello"
Tips for Building Muscle Memory
- Practice in short bursts (15-20 minutes) rather than long sessions
- Focus on accuracy first, then speed - sloppy practice builds sloppy habits
- Use hints sparingly - try at least once without them first
- Say the code out loud as you type it to engage more of your brain
- If you get stuck on the same part repeatedly, isolate and drill just that part
- Track your times - seeing improvement is motivating
Code Execution Requirement
These interactive drills require Judge0 to execute your code. If you see connection errors when running tests, make sure Judge0 is running locally or that the JUDGE0_API_URL environment variable is configured correctly.