Python Interview Q&A: Essential Concepts for Beginners
Python Interview Questions And Answers For Beginners
Overview
The image provides a summary of common Python interview questions and their corresponding solutions. The questions cover a variety of basic Python topics, useful for beginners preparing for interviews. Each section includes a specific question followed by the Python code that implements the solution. Here is a breakdown of the provided information.
Questions and Answers
Check if list contains integer x
l = [3, 4, 3, 5, 2, 111, 5]
print(111 in l) # True
- Thoughts: This simple check utilizes Python’s
in
keyword to determine if a particular element is present in a list.
Find duplicate number in integer list
def find_duplicates(elements):
duplicates, seen = set(), set()
for element in elements:
if element in seen:
duplicates.add(element)
seen.add(element)
return list(duplicates)
- Explanation: The function identifies duplicates in a list by using two sets: one for seen elements and one for duplicates. This is efficient due to the quick look-up properties of a set.
Check if two strings are anagrams
def is_anagram(s1, s2):
return set(s1) == set(s2)
print(is_anagram("elvis", "lives")) # True
- Idea: This function checks if two strings contain the same characters by comparing their sets. Note that this approach does not account for character frequency.
Remove all duplicates from list
lst = list(range(10)) + list(range(10))
lst = list(set(lst))
print(lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- Explanation: This snippet removes duplicates by converting the list to a set, then back to a list. This is a quick way to remove duplicates but does not maintain the original order.
Find pairs of integers in list so that their sum is equal to integer x
def find_pairs(l, x):
pairs = []
for (i, el1) in enumerate(l):
for (j, el2) in enumerate(l[i+1:]):
if el1 + el2 == x:
pairs.append((el1, el2))
return pairs
- Explanation: The function iterates through each pair of elements in the list to find those pairs whose sum equals a specified value
x
.
Get missing number in [1...100]
def get_missing_number(lst):
return set(range(1, lst[len(lst)-1]+1)) - set(lst)
l = list(range(1, 100))
l.remove(50)
print(get_missing_number(l)) # {50}
- Thoughts: This function finds the missing number in a sequence by comparing sets of the expected and actual numbers.
Compute the intersection of two lists
def intersect(lst1, lst2):
res, lst2_copy = [], lst2[:]
for el in lst1:
if el in lst2_copy:
res.append(el)
lst2_copy.remove(el)
return res
- Concept: The function computes the intersection by checking if elements of the first list are in the second list, and adding those to the result list.
Find max and min in unsorted list
l = [4, 3, 6, 3, 4, 888, 1, -11, 22, 3]
print(max(l)) # 888
print(min(l)) # -11
- Explanation: The
max
andmin
functions are used to find the largest and smallest elements in the list.
Reverse string using recursion
def reverse(string):
if len(string) <= 1: return string
return reverse(string[1:]) + string[0]
print(reverse("hello")) # olleh
- Concept: This recursive function reverses a string by removing the first character, reversing the rest, and appending the first character at the end.
Compute the first n Fibonacci numbers
a, b = 0, 1
n = 10
for i in range(n):
print(b)
a, b = b, a + b
# 1, 1, 2, 3, 5, 8, ...
- Explanation: The code uses a simple loop to compute and print the first
n
Fibonacci numbers.
Check if a string is a palindrome
def is_palindrome(phrase):
return phrase == phrase[::-1]
print(is_palindrome("anna")) # True
- Idea: This function checks if a string is equal to its reverse to determine if it is a palindrome.
Sort list with Quicksort algorithm
def qsort(L):
if L == []: return []
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, 55, 77, 999]
print(qsort(lst))
# [2, 33, 44, 55, 77, 999]
- Explanation: This code snippet sorts a list using the Quicksort algorithm, a divide-and-conquer sorting method.
Use list as stack, array, and queue
# as a list ...
l = [3, 4, 5, 6]
# as a stack ...
l.append(10) # l = [3, 4, 5, 6, 10]
l.pop() # l = [4, 5, 6]
# as a queue ...
l.insert(0, 5) # l = [5, 4, 5, 6]
l.pop() # l = [5, 4, 5]
- Concept: The same list can be manipulated to behave like a stack or queue, demonstrating Python’s list versatility.
Find all permutation of string
def get_permutations(w):
if len(w) <=1: return set(w)
smaller = get_permutations(w[1:])
perms = set()
for x in smaller:
for pos in range(0, len(x)+1):
perm = x[:pos] + w[0] + x[pos:]
perms.add(perm)
return perms
print(get_permutations("nan"))
# {'nna', 'ann', 'nan'}
- Explanation: This recursive function generates all permutations of a given string using a set to store and return unique permutations.
Reference:
www.geeksforgeeks.org
Top 50+ Python Interview Questions and Answers (Latest 2024)
www.interviewbit.com
Top Python Interview Questions and Answers (2024) - InterviewBit
www.simplilearn.com
Top 147+ Python Interview Questions You Must Know in 2024