Visit Desired Attractions Without Reusing a Trail
Updated
Problem statement
You are planning a camping walk from Parking Lot to Campsite. Each row of trails is one trail between two attractions, and you can walk it in either direction. Every row is its own trail. If two rows join the same two attractions, they are two separate trails, and you may use each of them once.
Return whether there is a walk that:
- starts at
Parking Lot; - ends at
Campsite; - visits every name in
desiredAttractionsat least once; and - uses each trail row at most once.
You may visit an attraction more than once. Reaching Campsite early does not end the walk. You may leave it and come back later, as long as you still have unused trails to walk.
Function
canVisitAllAttractions(trails: String[][], desiredAttractions: String[]) → booleanExamples
Example 1
trails = [["Beaver Dam","Frozen Ocean"],["Beaver Dam","Frozen Ocean"],["Parking Lot","Beaver Dam"],["Parking Lot","Liberty Lake"],["Beaver Dam","Campsite"],["Eel Weir","Campsite"],["Eel Weir","Campsite"]]desiredAttractions = ["Frozen Ocean"]return = trueOne walk that works is Parking Lot → Beaver Dam → Frozen Ocean → Beaver Dam → Campsite. It reaches Frozen Ocean on one Beaver Dam–Frozen Ocean trail and comes back on the other, so no trail is used twice. The answer is true.
Example 2
trails = [["Beaver Dam","Frozen Ocean"],["Beaver Dam","Frozen Ocean"],["Parking Lot","Beaver Dam"],["Parking Lot","Liberty Lake"],["Beaver Dam","Campsite"],["Eel Weir","Campsite"],["Eel Weir","Campsite"]]desiredAttractions = ["Liberty Lake","Beaver Dam"]return = falseLiberty Lake has only one trail, the one to Parking Lot. The walk must end at Campsite, so after visiting Liberty Lake it has to leave on that same trail. That uses the trail twice, which is not allowed, so the answer is false.
Example 3
trails = [["Beaver Dam","Frozen Ocean"],["Beaver Dam","Frozen Ocean"],["Parking Lot","Beaver Dam"],["Parking Lot","Liberty Lake"],["Beaver Dam","Campsite"],["Eel Weir","Campsite"],["Eel Weir","Campsite"]]desiredAttractions = ["Eel Weir"]return = trueOne walk that works is Parking Lot → Beaver Dam → Campsite → Eel Weir → Campsite. It reaches Eel Weir on one of the two Eel Weir–Campsite trails and comes back on the other. The answer is true.
Constraints
1 <= trails.length <= 20.- Every trail row holds exactly two attraction names, and neither name is empty.
- Two rows may join the same two attractions. They still count as separate trails.
0 <= desiredAttractions.length <= 15. A name may appear more than once in this list, but you only need to visit it once.- Every desired attraction appears in at least one trail row.
More Duolingo Phone Screen questions
- Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
- Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
- Select the Top User MatchesPHONE SCREEN · Seen Oct 2026
- Sum All Values in a Binary TreePHONE SCREEN · Seen Oct 2026
- User Resource Transition ProbabilitiesPHONE SCREEN · Seen Oct 2026
- Encrypt and Decrypt StringsPHONE SCREEN · Seen Apr 2026
- Validate a Mahjong Hand PartitionPHONE SCREEN · Seen Nov 2025
- Directional Tiles: Find a Valid Reconfiguration PathPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Attraction Route Without Reusing TrailsWhatNot · PHONE SCREEN
- Enumerate Directed Paths and CyclesByteDance · PHONE SCREEN
- Maximum Currency Conversion Along a Simple PathRippling · PHONE SCREEN · ONSITE INTERVIEW
- Three-Color an Undirected GraphSnowflake · PHONE SCREEN
- Count Prefix Paths in a Character GraphGoogle · PHONE SCREEN
- All Paths From Source to TargetBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Delivery ServiceInMobi · OA
- Longest Anchored Song ChainIntuit · OA
- Park Train Cars by PreferenceMercor · ONSITE INTERVIEW
- Find Trapped ZonesServiceNow · PHONE SCREEN