Rooms With Two Other Entrances and a Treasure Successor
Updated
Problem statement
Each row of instructions is [sourceRoom,destinationRoom], describing the one next-room instruction in the source room. treasureRooms identifies rooms containing treasures.
Return every room that satisfies both conditions: at least two other rooms immediately point to it, and its own instruction immediately points to a treasure room. A self-loop does not count as another incoming room, but it is a valid outgoing instruction to a treasure room. Reaching treasure after several instructions is insufficient.
For this exercise, assume a complete table with one next-room instruction for each room, no duplicate source rows, and every destination and treasure label represented. Cycles are allowed. Listing the same treasure room more than once has no extra effect.
For this exercise, assume each qualifying room appears once in the result, in case-sensitive ASCII lexicographic order. Empty input returns an empty result.
Function
filterRooms(instructions: String[][], treasureRooms: String[]) → String[]Examples
Example 1
instructions = [["jasmin","tulip"],["lily","tulip"],["tulip","tulip"],["rose","rose"],["violet","rose"],["sunflower","violet"],["daisy","violet"],["iris","violet"]]treasureRooms = ["lily","tulip","violet","rose"]return = ["tulip","violet"]Tulip has two other incoming rooms and points to itself, a treasure room. Violet has three other incoming rooms and points directly to treasure room rose.
Example 2
instructions = [["jasmin","tulip"],["lily","tulip"],["tulip","tulip"],["rose","rose"],["violet","rose"],["sunflower","violet"],["daisy","violet"],["iris","violet"]]treasureRooms = ["lily","jasmin","violet"]return = []Tulip and violet still have enough other incoming rooms, but their immediate destinations are not in this treasure list.
Constraints
- For this exercise, assume
0 <= instructions.length, treasureRooms.length <= 120. - Each instruction row contains exactly two room names, each a nonempty printable ASCII string of length
1through10. - Each source occurs once, and every destination has its own instruction row. Treasure labels name represented rooms and may repeat.
- Self-loops and longer cycles are valid; empty instructions require an empty treasure list.
More Walmart Phone Screen questions
- Count Months Starting on SundayPHONE SCREEN · Seen Aug 2026
- Longest Even-Length WordPHONE SCREEN · Seen Aug 2026
- Range ModulePHONE SCREEN · Seen Aug 2026
- Meeting Rooms IIPHONE SCREEN · Seen Aug 2026
- Sort Real Number StringsPHONE SCREEN · Seen Jan 2026
- Check a Password Against a Supplied PolicyPHONE SCREEN · Seen Jun 2025
- Convert a Restricted Function to an Arrow FunctionPHONE SCREEN · Seen Jun 2025
- Repair One Missing Function Punctuation CharacterPHONE SCREEN · Seen Jun 2025
- Can the Singer Perform the Song?PHONE SCREEN · Seen Mar 2025
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Aug 2022
Similar questions at other companies
- Parent and Child Counts for Every NodeKarat · PHONE SCREENKarat interview questions
- People with Zero or One ParentIndeed · PHONE SCREENIndeed interview questions
- Nodes With Zero or Three ParentsCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Top K Users by Distinct ContactsGoogle · PHONE SCREENGoogle interview questions
- Assign Campers to the First Arriving CarAtlassian · PHONE SCREENAtlassian interview questions
- Resource Transition ProbabilitiesWhatNot · PHONE SCREENWhatNot interview questions
- Find Every Reachable Derived Vocabulary WordDuolingo · PHONE SCREENDuolingo interview questions
- Order Employees by a Reports-To HierarchyBloomberg LP · PHONE SCREENBloomberg LP interview questions