Opendoor Coding Question

Anonymous User
378
Sep 13, 2024
Sep 13, 2024

2 questions to be solved in an hour; code must be runnable.

First Part

You are tasked with building a search engine for baby names. The engine collects baby names from various published lists and stores them in a JSON object. Each key in the JSON represents the source list name (e.g., "2015-us-official-boys", "2015-baby-center-girls"), and the corresponding value is a list of names ordered by popularity.

Example of JSON data:

{
    "2016-baby-center-girls": ["Sophia", "Emma", "Olivia", ...],
    "2016-baby-center-boys": ["Jackson", "Aiden", "Lucas", ...],
    "2015-baby-center-girls": ["Sophia", "Emma", "Olivia", ...]
}

Task

You need to write a function that, given a name, returns a list of all the lists (datasets) where the name appears, along with its rank in each list. The result should be sorted in ascending order by rank.

Example

Given the input "Sophia", your function should return:

[
    {"list": "2016-baby-center-girls", "rank": 1},
    {"list": "2015-baby-center-girls", "rank": 1},
    {"list": "2015-us-official-girls", "rank": 3}
]

Second Part

We want to improve the baby names search engine to make it more user-friendly. Now, instead of searching for an exact name, you need to implement a feature that allows users to search for names by a prefix. For example, a user may input a prefix like "an", and the search engine should return all baby names that start with that prefix (e.g., "anna", "anthony", etc.) along with their corresponding list name and relative ranking.

Example:

Given the prefix "an", the function should return a result like this:

{
    "anna": [
        {"list": "2015-baby-center-girls", "rank": 36},
        {"list": "2016-baby-center-girls", "rank": 46}
    ],
    "andrew": [
        {"list": "2015-baby-center-boys", "rank": 44},
        {"list": "2016-baby-center-boys", "rank": 47}
    ],
    "anthony": [
        {"list": "2015-baby-center-boys", "rank": 48}
    ]
}

Task:

  1. Write a function that takes a name prefix as input and returns all names starting with that prefix.
  2. For each name, return the list(s) where the name appears and the rank within those lists.
  3. Ensure that the result is in a format where the keys are the names that match the prefix, and the values are lists containing the dataset and rank information.

Additional Details:

  • The dataset format remains the same as in the previous problem, where names are listed in a JSON-like structure and are sorted by popularity.
  • The prefix search should be case-insensitive.

Comments (3)