0% found this document useful (0 votes)
327 views

CheatSheet Python 6 Coding Interview Questions

This document contains 14 Python interview questions and their solutions. Each question is presented with a short description and code snippet. The questions cover a range of common Python topics like lists, strings, recursion, sorting, and more.

Uploaded by

sadeq moghaddasi
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)
327 views

CheatSheet Python 6 Coding Interview Questions

This document contains 14 Python interview questions and their solutions. Each question is presented with a short description and code snippet. The questions cover a range of common Python topics like lists, strings, recursion, sorting, and more.

Uploaded by

sadeq moghaddasi
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
You are on page 1/ 1

Python Cheat Sheet: 14 Interview Questions 

“​A puzzle a day to learn, code, and play​” → Visit ​finxter.com 

Question Code Question Code

Check if list l = [​3​, ​3​, ​4​, ​5​, ​2​, ​111​, ​5​] Get missing def​ g​ et_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​))
l.remove(​50​)
print(get_missing_number(l)) ​# 50 

Find duplicate def​ ​find_duplicates​(elements): Compute def​ i ​ ntersect​(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:
duplicates.add(element) res.append(el)
seen.add(element) lst2_copy.remove(el)
​return​ list(duplicates)  ​return​ res

Check if two def​ i​ s_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​: r
​ eturn​ 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
pairs.append((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​, 2​ 2​, 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()
l.append(​10​) ​# l = [4, 5, 6, 10] ​for​ x ​in​ smaller:
l.pop() ​# l = [4, 5, 6] ​for​ pos ​in​ range(​0,
​ len(x)+​1​):
perm = x[:pos] + w[​0​] + x[pos:]
# ... and as a queue perms.add(perm)
l.insert(​0​, ​5​) ​# l = [5, 4, 5, 6] ​return​ perms
l.pop() ​# l = [5, 4, 5]  print(get_permutations(​"nan"​))
# {'nna', 'ann', 'nan'}

You might also like