UBER'S #1 INTERVIEW QUESTION | BUS ROUTES | PYTHON BFS SOLUTION

Поділитися
Вставка
  • Опубліковано 26 лис 2024

КОМЕНТАРІ • 15

  • @samarthinani3870
    @samarthinani3870 2 роки тому +2

    This is so well explained and neatly written. Thanks a ton

  • @SatinderSingh71
    @SatinderSingh71 11 місяців тому +1

    No need to store the route length inside the queue. Just record the route_len as a variable that gets incremented for every queue iteration

  • @ebenezeracquah478
    @ebenezeracquah478 Рік тому

    Your explanation is great.

  • @suryapasupuleti60
    @suryapasupuleti60 Місяць тому

    I dont get how incrementing the route_len by 1 makes sense cause we would then be finding gate number of stops rather than the number of buses taken right?

    • @UrslurStarSky7
      @UrslurStarSky7 Місяць тому +1

      I think the understanding is that the route length is only incremented when we have a new bus to travel in, i.e one that is not in v_buses - so when you increment 1 you're incrementing the number of buses you've taken in that path.

  • @YummyMrPie
    @YummyMrPie 3 місяці тому

    How come we make an adjacency list of bus stops to bus routes rather than bus stops to other bus stops?

  • @xl8825
    @xl8825 Рік тому +1

    "Least number of buses" is not the same as "shortest path or least number of stops", my understanding is we are trying to find a path with least switch between buses, I wonder why BFS would work

    • @Ybby999
      @Ybby999 11 місяців тому

      The number of buses is just the amount of stops plus one, right?

  • @ziqinwu2337
    @ziqinwu2337 7 місяців тому

    great video!

  • @sharathkumar8338
    @sharathkumar8338 2 роки тому

    Is there a pattern as in when to use BFS and DFS???

    • @crackfaang
      @crackfaang  2 роки тому +2

      Typically you use BFS for when you want the shortest path, DFS for when you need to explore all paths. If the problem can be solved using either, I always prefer BFS because I find it easier to code and you don’t have to worry about overflow in extreme cases

  • @nickold4499
    @nickold4499 2 роки тому

    I tried your solution but unfortunately I got a TLE. Not sure what to do about that

    • @crackfaang
      @crackfaang  2 роки тому

      Hmm, did you mess up an indentation somewhere? I recall getting a TLE due to an indentation issue

    • @YT.Nikolay
      @YT.Nikolay 2 роки тому

      @@crackfaang I have optimized it by: 1 - check if the target exists before you started BFS (case #44?), 2 - check for visited before adding anything into the queue. This fixed all TLE problems for me

  • @subee128
    @subee128 10 місяців тому

    Thanks