0% found this document useful (0 votes)
5 views1 page

CheatSheet Python 6 Coding Interview Questions

This document is a Python cheat sheet that presents 14 common interview questions along with their corresponding code solutions. It covers topics such as checking for duplicates, finding missing numbers, determining anagrams, and implementing sorting algorithms. Each question is accompanied by a brief code snippet demonstrating the solution.

Uploaded by

idunkoncode
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

CheatSheet Python 6 Coding Interview Questions

This document is a Python cheat sheet that presents 14 common interview questions along with their corresponding code solutions. It covers topics such as checking for duplicates, finding missing numbers, determining anagrams, and implementing sorting algorithms. Each question is accompanied by a brief code snippet demonstrating the solution.

Uploaded by

idunkoncode
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Cheat Sheet: 14 Interview Questions

“A puzzle a day to learn, code, and play” → Visit [Link]

Question Code Question Code

Check if list l = [3, 3, 4, 5, 2, 111, 5] Get missing def get_missing_number(lst):


contains print(111 in l) # True number in return set(range(lst[len(lst)-1])[1:]) - set(l)
integer x [1...100] l = list(range(1,100))
[Link](50)
print(get_missing_number(l)) # 50

Find duplicate def find_duplicates(elements): Compute def intersect(lst1, lst2):


number in duplicates, seen = set(), set() the res, lst2_copy = [], lst2[:]
integer list for element in elements: intersection for el in lst1:
if element in seen: of two lists if el in lst2_copy:
[Link](element) [Link](el)
[Link](element) lst2_copy.remove(el)
return list(duplicates) return res

Check if two def is_anagram(s1, s2): Find max l = [4, 3, 6, 3, 4, 888, 1, -11, 22, 3]
strings are return set(s1) == set(s2) and min in print(max(l)) # 888
anagrams print(is_anagram("elvis", "lives")) # True unsorted list print(min(l)) # -11

Remove all lst = list(range(10)) + list(range(10)) Reverse def reverse(string):


duplicates from lst = list(set(lst)) string using if len(string)<=1: return string
list print(lst) recursion return reverse(string[1:])+string[0]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(reverse("hello")) # olleh

Find pairs of def find_pairs(l, x): Compute a, b = 0, 1


integers in list pairs = [] the first n n = 10
so that their for (i, el_1) in enumerate(l): Fibonacci for i in range(n):
sum is equal to for (j, el_2) in enumerate(l[i+1:]): numbers print(b)
integer x if el_1 + el_2 == x: a, b = b, a+b
[Link]((el_1, el_2)) # 1, 1, 2, 3, 5, 8, ...
return pairs

Check if a def is_palindrome(phrase): Sort list with def qsort(L):


string is a return phrase == phrase[::-1] Quicksort if L == []: return []
palindrome print(is_palindrome("anna")) # True algorithm return qsort([x for x in L[1:] if x< L[0]]) + L[0:1] +
qsort([x for x in L[1:] if x>=L[0]])
lst = [44, 33, 22, 5, 77, 55, 999]
print(qsort(lst))
# [5, 22, 33, 44, 55, 77, 999]

Use list as # as a list ... Find all def get_permutations(w):


stack, array, l = [3, 4] permutation if len(w)<=1:
and queue l += [5, 6] # l = [3, 4, 5, 6] s of string return set(w)
smaller = get_permutations(w[1:])
# ... as a stack ... perms = set()
[Link](10) # l = [4, 5, 6, 10] for x in smaller:
[Link]() # l = [4, 5, 6] for pos in range(0,len(x)+1):
perm = x[:pos] + w[0] + x[pos:]
# ... and as a queue [Link](perm)
[Link](0, 5) # l = [5, 4, 5, 6] return perms
[Link]() # l = [5, 4, 5] print(get_permutations("nan"))
# {'nna', 'ann', 'nan'}

You might also like