Google Foobar Level 1

March 8, 2022

I'll spare you the backstory in each challenge and cut right to the meat of the quiz. Here goes:

The Prison Labor Dodges

Time alloted: 7 days

Given two almost identical lists of numbers x and y, where one of the lists contains an additional ID, write a function solution(x, y) that compares the lists and returns the additional ID.

For example, given the lists x = [13, 5, 6, 2, 5] and y = [5, 2, 5, 13], the function solution(x, y) would return 6 because the list x contains the integer 6 and the list y doesn't.

In each test case, the lists x and y will always contain n non-unique integers where n is at least 1 but never more than 99, and one of the lists will contain an additional unique integer which should be returned by the function. The same n non-unique integers will be present on both lists, but they might appear in a different order, like in the examples above. All numbers in the list will be between -1000 and 1000.

The problem wants us to compare 2 lists, where one is larger than the next by one element. Pretty straightforward. The lists can be contain up to 100 elements. Here's the algorithm I used to solve this challenge

Here's the entire solution:

    # Solution to the Foobar Level 1 challenge: 
    def solution(x,y):
      from collections import Counter
      larger = y if (len(y) > len(x)) else x
      smaller = x if (len(y) > len(x)) else y
      if (len(y) == len(x)):
        return ''
      else:
        return list(Counter(larger)-Counter(smaller))[0]

    if __name__ == "__solution__":
      solution()

For questions or suggestions, please DM me @sandeep_gopal.

UPDATE:
I'll post the full list of solutions here Meet Google Foobar.

Share