Lesson 294 of 296
Mock interview 1: JavaScript and front-end round
What you will learn
- How a typical 60-minute front-end round runs
- Answering quick-fire JavaScript and React questions
- A live coding task, solved out loud step by step
- Handling follow-up questions
On this page
How the round runs
| Time | Part |
|---|---|
| 5 min | Introductions, "Tell me about yourself" (Lesson 290) |
| 15 min | Quick-fire questions on JavaScript, TypeScript and React |
| 30 min | Live coding |
| 10 min | Your questions (Lesson 293) |
Part 1: quick-fire questions
Answer in two or three sentences, then stop. The interviewer will ask for more if they want it.
Q: What is the difference between == and ===?
"=== compares value and type with no conversion. == converts types first, so 0 == '' is true. I always use ===." (Lesson 34)
Q: What is a closure?
"A function that remembers the variables from the scope where it was created, even after that scope has finished. It is how private state, debounce and React hooks work." (Lesson 53)
Q: What will this print?
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
// A, D, C, B"A and D first, because synchronous code runs first. Then C, because promise callbacks are microtasks, which run before the next task. Then B, the timer callback, which is a task." (Lesson 68)
Q: What is the difference between interface and type in TypeScript?
"Both describe object shapes. Interfaces can be extended and merged by declaring them again; type aliases can also describe unions, tuples and other types. For object shapes either is fine; I follow the team's convention." (Lesson 104)
Q: Why does React need a key on list items?
"So React can match items between renders. With a stable unique key, such as an id, it keeps the right state on the right item when the list changes. Using the array index breaks this when items are added, removed or reordered." (Lesson 162)
Q: When does useEffect run, and what is the clean-up for?
"After the component renders, and again when a value in the dependency array changes. The clean-up runs before the next effect and when the component is removed, to cancel timers, subscriptions or stale fetch requests." (Lesson 166)
Q: How would you make a slow page faster?
"First measure: Lighthouse, the Performance tab and Core Web Vitals, to find the real cause. Then fix the biggest cause: large JavaScript bundles with code splitting, unoptimised images, too many re-renders with memoisation, or slow API calls with caching. Measure again after each change." (Lessons 86 and 172)
Part 2: live coding
Interviewer: "We have a list of purchases. Write a function that returns each customer's total spending, sorted from highest to lowest. Then return only the top N customers."
type Purchase = { customerId: string; amountPence: number };Step 1: repeat the problem and ask questions (2 minutes).
"So I get an array of purchases and return customers with their totals, highest first. A few questions: can the array be empty? Can amounts be negative, for refunds? If two customers have the same total, does the order matter? Are amounts always whole pence?"
Interviewer: "It can be empty. Negative amounts are refunds and should count. Ties: sort by customer id. Amounts should be whole numbers; if not, throw an error."
Step 2: explain the plan before coding (1 minute).
"I'll loop once and add each amount into a Map from customer id to total, which is O(n). Then I'll turn the Map into an array and sort it by total descending, then id ascending. Sorting is O(k log k) for k customers. Top N is just a slice of the sorted array."
Step 3: write the code while talking.
type Purchase = { customerId: string; amountPence: number };
type CustomerTotal = { customerId: string; totalPence: number };
function totalsByCustomer(purchases: Purchase[]): CustomerTotal[] {
const totals = new Map<string, number>();
for (const p of purchases) {
if (!Number.isInteger(p.amountPence)) {
throw new Error(`Invalid amount for ${p.customerId}: ${p.amountPence}`);
}
totals.set(p.customerId, (totals.get(p.customerId) ?? 0) + p.amountPence);
}
return [...totals.entries()]
.map(([customerId, totalPence]) => ({ customerId, totalPence }))
.sort((a, b) => b.totalPence - a.totalPence || a.customerId.localeCompare(b.customerId));
}
function topCustomers(purchases: Purchase[], n: number): CustomerTotal[] {
if (n <= 0) return [];
return totalsByCustomer(purchases).slice(0, n);
}
const purchases: Purchase[] = [
{ customerId: "c2", amountPence: 500 },
{ customerId: "c1", amountPence: 300 },
{ customerId: "c2", amountPence: -100 },
{ customerId: "c3", amountPence: 400 },
{ customerId: "c1", amountPence: 100 },
];
console.log(totalsByCustomer(purchases));
// [ { customerId: 'c1', totalPence: 400 }, { customerId: 'c2', totalPence: 400 }, { customerId: 'c3', totalPence: 400 } ]
console.log(topCustomers(purchases, 1)); // [ { customerId: 'c1', totalPence: 400 } ]
console.log(totalsByCustomer([])); // []Step 4: test out loud.
"Let me check my example: c2 is 500 minus 100, so 400; c1 is 300 plus 100, so 400; c3 is 400. All tie, so they're sorted by id: c1, c2, c3. That's correct. Edge cases: an empty array returns an empty array; n of zero returns an empty array; a decimal amount throws."
Step 5: follow-up questions.
Interviewer: "What if there are a million purchases and we only need the top 10?"
"Building the totals is still O(n), and we must read every purchase. But sorting all customers is wasteful. I could keep a min-heap of size 10 (Lesson 140): for each customer, add them, and if the heap has more than 10, remove the smallest. That makes the selection O(k log 10), basically O(k). In practice, with data in a database, I would let SQL do it with GROUP BY, ORDER BY and LIMIT, with an index."
Interviewer: "Why store pence instead of pounds?"
"Floating point numbers cannot store many decimal values exactly; 0.1 plus 0.2 is not 0.3. Whole numbers of the smallest unit avoid rounding errors in money." (Lesson 49)
Interviewer: "How would you show this in a React component that updates as the user types a filter?"
"I'd keep the purchases from the API in server state, keep the filter text in useState, and compute the filtered totals during render, wrapped in useMemo only if measuring shows it is slow. If the filter calls the API, I'd debounce the input and cancel stale requests in the effect's clean-up."
What made this answer strong
- Asked questions before coding, and wrote down the rules.
- Explained the plan and the complexity first.
- Used clear names and handled edge cases.
- Tested with the example out loud and found the tie case.
- Answered follow-ups by discussing trade-offs, not only code.
How to practise this lesson
Cover the answers. Set a 30-minute timer and solve the coding task out loud, saying each step. Then answer each quick-fire question in under 30 seconds. Record yourself if you can, and listen back for long silences.
Key words
| Word | Meaning |
|---|---|
| Quick-fire questions | Short questions with short answers |
| Think out loud | Saying your reasoning while you work |
| Edge case | An unusual input, such as empty, zero or negative |
| Follow-up | A harder question building on your answer |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.