Your question is Debug Code With Reasoning. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
BNSF Railway's dispatch team uses an internal dashboard to track railcars on a given train. A candidate for Frontend Engineer was shown this component and asked to debug it while narrating their thought process out loud.
import { useEffect, useState } from "react";
interface Railcar {
id: string;
status: string;
lastStation: string;
}
function ShipmentTracker({ trainId }: { trainId: string }) {
const [railcars, setRailcars] = useState<Railcar[]>([]);
const [delayedCount, setDelayedCount] = useState(0);
useEffect(() => {
fetchRailcars(trainId).then((data) => {
setRailcars(data);
const delayed = data.filter((car) => car.status === "DELAYED");
setDelayedCount(delayed.length);
});
});
function markInspected(id: string) {
const car = railcars.find((c) => c.id === id);
car.status = "INSPECTED";
setRailcars(railcars);
}
return (
<ul>
{railcars.map((car, index) => (
<li key={index}>
{car.id} - {car.status}
<button onClick={() => markInspected(car.id)}>Inspect</button>
</li>
))}
</ul>
);
}
export default ShipmentTracker;
Debug this component out loud: what would you expect to actually happen when it renders and when a dispatcher clicks "Inspect," and what would you change and why?