AMAZON | PHONE INTERVIEW | PathWithKStops
Anonymous User
806

Given a list of tuples where there is two strings in each tuple (from, to) return True if a trip can be made using EXACTLY k flights else return False.

def PathWithKStops (boardingPasses, origin, dst, k):

Example Input #1:

boardingPasses = [ ('SEA','SLC'), ('SLC', 'SEA'), ('SEA', 'NYC'), ('NYC', 'SLC'), ('SEA', 'SLC') ]
origin = 'SEA'
dst = 'SEA'
k = 3

Output:
returns True because you would go from SEA->NYC, NYC->SLC, SLC->SEA so exactly 3 flights.

Notice:

  • You don't need to use all the boarding passes
  • You can have duplicates in boardingPasses (in example input #1 it's ('SEA', 'SLC') so there's two ('SEA', 'SLC') passes)
  • The from will never be the same as the to (i.e. [ ('SEA', 'SEA') ] is NOT valid input)
  • The origin can be the same as the destination (i.e. origin='SEA' , dst='SEA' is valid)
  • You can only use each boarding pass once (in example input #1 there is 5 passes)

Example Input #2 :

boardingPasses =  [ ('SEA', 'SLC'), ('SLC', 'NYC'), ('SEA', 'NYC'), ('NYC', 'SEA') ]
src = 'NYC'
dst = 'SEA'
k = 2

Output:
returns False because you can't go from NYC to SEA in exactly 2 flights

Would appreciate any help on how to approach this problem, I still can't figure out how to solve it.

UPDATE: I FINALLY SOLVED IT

Comments (4)