Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Debugging Provided Code

MediumCoding00:00
Practice interviewer
In session
5 left
00:00

Your question is Debugging Provided Code. 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).

You need to log in / sign up to chat or submit.

Problem

Via Transportation shows dispatchers a live queue of ride requests for a service zone, polling the backend every few seconds and letting a dispatcher click a request to flag the one with the shortest ETA. Here is the component as it currently exists.

import { useEffect, useState } from "react";

interface RideRequest {
  id: string;
  riderName: string;
  etaMinutes: number;
}

function LiveRideQueue({ zoneId }: { zoneId: string }) {
  const [requests, setRequests] = useState<RideRequest[]>([]);
  const [selectedId, setSelectedId] = useState("");

  useEffect(() => {
    const interval = setInterval(() => {
      fetchRequestsForZone(zoneId).then((data) => {
        requests.push(...data);
        setRequests(requests);
      });
    }, 5000);
  }, []);

  function selectFastest() {
    const sorted = requests.sort((a, b) => a.etaMinutes - b.etaMinutes);
    setSelectedId(sorted[0].id);
  }

  return (
    <ul>
      {requests.map((req, index) => (
        <li key={index} onClick={selectFastest}>
          {req.riderName} - {req.etaMinutes} min {req.id === selectedId && "(selected)"}
        </li>
      ))}
    </ul>
  );
}

What is the issue in this component? Walk through everything wrong with it and explain what you would change and why.