Find Minimum City Hops
Updated
Problem statement
You are given an array of cities named cities. Each city is represented by a three-character lowercase string.
Two cities are directly connected when their strings differ in exactly one character position.
Starting from startCity, you may hop only between directly connected cities in cities. Return the minimum number of hops needed to reach endCity. If endCity is not reachable, return -1.
Function
findMinimumCityHops(cities: String[], startCity: String, endCity: String) → intExamples
Example 1
cities = ["abc","abd","acd","xyz"]startCity = "abc"endCity = "acd"return = 2One shortest route is abc to abd to acd. Each hop changes exactly one character, so the route uses 2 hops.
Example 2
cities = ["aaa","aab","abb","bbb"]startCity = "aaa"endCity = "bbb"return = 3The route aaa to aab to abb to bbb changes one position per hop and reaches the destination in 3 hops.
Example 3
cities = ["abc","abd","xyz"]startCity = "abc"endCity = "xyz"return = -1The city xyz is not connected to the component containing abc, so it cannot be reached.
Constraints
citiescontains at least one city.- Every value in
cities, as well asstartCityandendCity, consists of exactly three lowercase English letters. startCityandendCityeach appear incities.
Source note: Amazon OA sighting shared by a FastPrep contributor.
More Amazon Online Assessment questions
- Get Smallest Base SegmentOA · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026
- Minimum Merge ConflictsOA · Seen Nov 2026
- Shopping Cart BillingOA · Seen Nov 2026
Similar questions at other companies
- Shortest Path from Encoded Graph EdgesBitGo · OABitGo interview questions
- Shortest Route Through a Traced MazeDuolingo · OADuolingo interview questions
- Minimum Genetic MutationBenchling · OABenchling interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
- Find a Directed Resource Access PathCoreWeave · PHONE SCREEN
- Word LadderGoogle · ONSITE INTERVIEWGoogle interview questions
- Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Shortest Word Transformation SequenceReddit · PHONE SCREENReddit interview questions
- Word LadderGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
Amazon interview experiences
- Amazon SWE OA with AI Coding: 1 Experience
- Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
- Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
- Amazon BIE Intern SQL and Interview Questions: 17 Experiences
- Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
- Amazon SDE II (L5) Interview Questions: 5 Experiences