Few tricky and challenging Python - Interview Problems with Answers !! Get link Facebook X Pinterest Email Other Apps ########################################################################## # Problem 1 (easy): first unique character in a string input_string = 'treetraversal' output = 'v' # Answer def first_unique(input_string): # You hav to write code here test_dict = {} unique_letters = set(list(input_string)) #print(unique_letters) for letter in unique_letters: if letter not in test_dict: test_dict[letter] = 0 for _ in list(input_string): if letter == _: test_dict[_] += 1 letters = [] for key in test_dict: if test_dict[key] == 1: letters.append(input_string.index(key)) letters = sorted(letters) return (input_string[letters[0]]) print(first_unique(input_string)) ########################################################################### # Problem 2 (easy): Group Anagrams entries = ['star','astr','car','rac','st'] output = [['star', 'astr'], ['car', 'rac'], ['st']] # Answer def group_anagrams(entries): results = [] temp_entries = entries[:] for entry in entries: temp_entries.remove(entry) found = '' if entries: for test_set in temp_entries: if set(list(entry)) == set(list(test_set)): found = test_set if found: entries.remove(found) results.append([entry, found]) if not found: results.append([entry]) return (results) #print(group_anagrams(entries)) ############################################################################ # Problem 3 (medium): Design a OOP class for imaginary number arithmetic # Notes that (write this out in math term on the whiteboard) # a + b = (a.r + b.r) + (a.i + b.i)i # a - b = (a.r - b.r) + (a.i - b.i)i # 2,3 --> 2 + 3i class Complex(object): def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary def __str__(self): return str(self.real) + " + " + str(self.imaginary) + "i" def __add__(self, _obj): return str(self.real + _obj.real) + " + " + str(self.imaginary + _obj.imaginary) + "i" def __sub__(self, _obj): return str(self.real - _obj.real) + " + " + str(self.imaginary - _obj.imaginary) + "i" a = Complex(1, 2) b = Complex(1, 3) print(a) # 1 + 2i print(b) # 2 + 3i print(a + b) print(a - b) ############################################################################ # Problem 4 (SQL): # Given a table (name, component, salary) with sample records: # chris, bonus, 1000 # chris, bonus, 500 (there can be multiple entries for bonuses) # chris, base, 10000 # adam, bonus, 5000 # adam, bonus, 50 # adam, base, 30000 # ... # # # Write SQL to return result as (name, bonus_salary, base_salary) # name, bonus_salary, base_salary # chris 1500 10000 # adam 5050 30000 # # Answer # WRITE SQL QUERY HERE... # # # How would you do this in a scripting language e.g. Python. You may assume records is array> # # Solution: data = [ {'name': 'chris', 'component': 'bonus', 'salary': 1000}, {'name': 'chris', 'component': 'bonus', 'salary': 500}, {'name': 'chris', 'component': 'base', 'salary': 10000}, {'name': 'adam', 'component': 'bonus', 'salary': 5000}, {'name': 'adam', 'component': 'bonus', 'salary': 50}, {'name': 'adam', 'component': 'base', 'salary': 30000}, ] output = [ {'name': 'chris', 'bonus_component': 1500, 'base_component': 10000}, {'name': 'adam', 'bonus_component': 5050, 'base_component': 30000}, ] # find the unique name keys names = [_['name'] for _ in data] names = list(set(names)) temp = {} # collect salary for name in names: temp[name] = {} for _ in data: if name == _['name']: component = _['component'] salary = _['salary'] if component not in temp[name]: temp[name][component] = salary else: temp[name][component] += salary # print(temp) final_output = [] # merge the values and make dict for key in temp: final_output.append(dict([('name', key), ('bonus_component', temp[key]['bonus']), ('base_component', temp[key]['base'])])) # result print(final_output) Comments
Comments
Post a Comment