Codepath

Find Key

JCSU Unit 2 Problem Set 1 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy-Medium
  • Time to complete: 15 mins
  • 🛠️ Topics: Nested Dictionaries, Iteration, Key Search

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What is the goal of the problem?
    • The goal is to find the value associated with a given key in a dictionary that contains nested dictionaries.
  • Are there constraints on input?
    • The input dictionary values will always be dictionaries themselves.

HAPPY CASE Input: nested_dict = { "a": {"name": "Alice", "age": 25}, "b": {"name": "Bob", "age": 30}, "c": {"name": "Charlie", "age": 35} } key = "name" Output: "Alice" Explanation: The key "name" is found in the nested dictionary "a", and its value is "Alice".

EDGE CASE Input: nested_dict = {"a": {}, "b": {}} key = "age" Output: None Explanation: The key "age" is not found in any nested dictionary.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For nested dictionary search problems, we want to consider the following approaches:

  • Nested Loops: Use a loop to iterate through the outer dictionary and a conditional check to look inside the nested dictionaries.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea:
Iterate through the outer dictionary and check if the key exists in each nested dictionary. If the key is found, return its value. If the loop completes without finding the key, return None.

Steps:

  1. Iterate through the outer dictionary using items() to access both keys and values.
  2. For each value, check if it is a dictionary.
  3. If it is a dictionary, check if the given key exists.
  4. If the key is found, return its associated value.
  5. If the loop completes without finding the key, return None.

4: I-mplement

Implement the code to solve the algorithm.

def find_key(nested_dict, key):
    for outer_key, inner_dict in nested_dict.items():  # Iterate through the outer dictionary
        if isinstance(inner_dict, dict):  # Check if the value is a nested dictionary
            if key in inner_dict:  # Check if the key exists in the nested dictionary
                return inner_dict[key]  # Return the value associated with the key
    return None  # Return None if the key is not found in any nested dictionary

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

Example 1:

  • Input: nested_dict = { "a": {"name": "Alice", "age": 25}, "b": {"name": "Bob", "age": 30}, "c": {"name": "Charlie", "age": 35} } key = "name"
  • Expected Output: "Alice"
  • Observed Output: "Alice"

Example 2:

  • Input: nested_dict = {"a": {}, "b": {}} key = "age"
  • Expected Output: None
  • Observed Output: None

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume n is the total number of outer keys, and m is the average number of keys in each nested dictionary.

  • Time Complexity: O(n * m) because we iterate through the outer dictionary and check each nested dictionary.
  • Space Complexity: O(1) because no additional data structures are created.
Fork me on GitHub