Airbnb questions

Review Token Tagging

AirbnbPhone screenMedium

A review site marks the names it knows. You get a review text and a map tokens that maps each token to a label. Write tag_review(review, tokens), which returns the review with every occurrence of a token wrapped as [label]{text}, where text is the occurrence exactly as it appears in the review.

Matching ignores case, so the token san francisco matches San Francisco, and the wrapped text keeps the casing of the review. A token may contain spaces and punctuation, and it matches only that exact sequence of characters (a single space matches a single space). A token matches anywhere, including inside a longer word. Everything outside matches is copied unchanged.

Part 1: Wrap each token

Return the tagged review. For example, with review = "I booked a house on Airbnb for my trip to San Francisco. It was a lovely experience." and tokens = {"Airbnb": "business", "san francisco": "city"}, the result is "I booked a house on [business]{Airbnb} for my trip to [city]{San Francisco}. It was a lovely experience.".

  1. Example 1
    review
    "I booked a house on Airbnb for my trip to San Francisco. It was a lovely experience."
    tokens
    {"Airbnb":"business","san francisco":"city"}
    Output
    "I booked a house on [business]{Airbnb} for my trip to [city]{San Francisco}. It was a lovely experience."

    Why: Airbnb matches itself, and san francisco matches San Francisco across the space. Each match keeps the casing the review used, so the label wraps San Francisco, not san francisco.

Constraints

  • 0 <= len(review) <= 10^4
  • 0 <= len(tokens) <= 1000
  • 1 <= len(token) <= 100 for every token, and 1 <= len(label) <= 20
  • The review, tokens and labels use printable ASCII characters only
  • No two tokens are equal when case is ignored
  • No two occurrences of tokens in the review share a character
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Review Token Tagging: Airbnb interview question | LoopCoach