Amazon Interview Question for Jr. Software Engineers






Comment hidden because of low score. Click to expand.
0
of 0 vote

Time complexity:
Symmetric Difference a^b: O(len(a) * len(b)) - worst case
Adding the for loop, it gives the final time complexity: O(len(a) * len(b) * len(k))
Where: a is dict_a, b is dict_b, and k is the unique key set from both dicts a and b.

def dict_comp(dict_a, dict_b):
    keys_a = set(dict_a.keys())
    keys_b = set(dict_b.keys())
    # Getting the set of elements which are in either of the sets but not in both.
    keys = sorted(keys_a.symmetric_difference(keys_b))
    # Set to store the unique values from both dicts
    values = set()
    dict_ab = {}
    # looping through the key's set
    for key in keys:
        # if key exists only in dict_a
        if key in dict_a:
            a_value = dict_a.get(key)
            # if the value already exists in the set, them remove it
            if a_value in values:
                values.remove(a_value)
            # else, save the value to the set, because it's a unique value
            else:
                values.add(a_value)
        # if key exists only in dict b
        elif  key in dict_b:
            b_value = dict_b.get(key)
            if b_value in values:
                values.remove(b_value)
            else:
                values.add(b_value)

    print("There are " + str(len(values)) + " elements with an unique pair of keys and values in both dicts")

- Lawrence Fernandes May 06, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More