FastPrepRooms With Two Other Entrances and a Treasure Successor

Rooms With Two Other Entrances and a Treasure Successor

Walmart logoWalmart● EasyFULLTIMEPHONE SCREEN
Learn

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 single-successor table with one row per represented room, no duplicate source rows, and every destination and treasure label represented. Cycles are allowed. Repeated treasure labels have membership semantics.

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 1 through 10.
  • 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 problems

See Walmart hiring insights
public String[] filterRooms(String[][] instructions, String[] treasureRooms) {
    // Write your code here.
    return new String[0];
}
instructions[["jasmin","tulip"],["lily","tulip"],["tulip","tulip"],["rose","rose"],["violet","rose"],["sunflower","violet"],["daisy","violet"],["iris","violet"]]
treasureRooms["lily","tulip","violet","rose"]
expected["tulip", "violet"]
Checking account…