Hey! I am Onur. Sorry, I was away for a while, but I’ll make it up to you with a pragmatic post that may help you to lend a better job. So, here’s a few Python interview questions for the ones looking for a job! By the way, I’m warning you, they are just junior level stuff that I get from “Cracking the Coding Interview” book.
Question-1: isUnique?
Implement an algorithm to determine if a string has all unique characters.
Here’s the Python code:
#To achieve this, we create a set with the letters of the string. If there is #no duplications, the length of set will be equal to the string. EZ, PZ!
def isUnique(strin):
setin = set(strin)
if len(setin) != len(strin):
return False
return True
Question-2: Check Permutation
Given two strings, write a method to decide if one is a permutation of the other.
Here’s the Python code:
#This function checks if str1 is a permutation of str2.
def check(str1, str2):
l1 = list(str1)
l2 = list(str2)
for a in l1:
if a in l2:
l2.remove(a)
else:
return False
return True
#This just compares the length of them to pass accordingly.
def checkPermutation(str1, str2):
if len(str1) > len(str2):
return check(str2, str1)
return check(str1, str2)
Question-3: URLify
Write a method to replace all spaces in a string with ‘%20: You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the “true” length of the string.
Here’s the Python code:
#This is so easy, it felt like cheating.
def urlify(input):
return input.replace(' ', '%20')
Question-4: Palindrome Permutation
Given a string, write a function to check if it is a permutation of a palin-drome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
Here’s the Python code:
#Make string smaller and smaller by removing doubles. If more than one #characters remained in the string at the end, a palindrome cannot be formed #from it.
def PP(lin):
if len(lin) < 2:
return True
for ch in lin:
if lin.count(ch) > 1:
lin.remove(ch)
lin.remove(ch)
return(PP(lin))
return False
#Need to remove spaces since spaces shouldn't be cared in palindromes.
def isPP(strin):
lin = list(strin)
lin = [value for value in lin if value != ' ']
return PP(lin)

Question-5: One Away
There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.
Here’s the Python code:
def isOneAway(str1, str2):
if str1 == str2:
#Same strings are zero edits away.
return True
if len(str1) > (len(str2) + 1) or len(str1) < (len(str2) - 1):
#If their lengths are so different, they couldn't be one edit away.
return False
errored = False
#One error can be tolerated, since it will be our one edit.
l1 = list(str1)
l2 = list(str2)
if len(str1) > len(str2):
for ch1n, ch1 in enumerate(l1):
try:
if ch1 == l2[ch1n - errored]:
continue
else:
if not errored:
errored = True
else:
return False
except IndexError:
if not errored:
errored = True
else:
return False
elif len(str2) > len(str1):
for ch2n, ch2 in enumerate(l2):
try:
if l1[ch2n - errored] == ch2:
continue
else:
if not errored:
errored = True
else:
return False
except IndexError:
if not errored:
errored = True
else:
return False
else:
for ch1n, ch1 in enumerate(l1):
if ch1 == l2[ch1n]:
continue
else:
if not errored:
errored = True
else:
return False
return True
Question-6: String Compression
Implement a method to perform basic string compression using the counts of repeated characters. For example, the string “aabcccccaaa” would become “a2b1c5a3”. If the “compressed” string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a -z).
Here’s the Python code:
def compress(strin):
last = ""
ret = ""
for a in strin:
if a == last:
count += 1
else:
if last == "":
last = a
count = 1
else:
ret += last
ret += str(count)
last = a
count = 1
ret += last
ret += str(count)
#Return original string if it is not compressed at all.
if len(ret) < len(strin):
return ret
return strin
Question-7: Rotate Matrix
Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees.
Here’s the Python code:
def rotatematrix(matrix):
width = len(matrix[0])
height = len(matrix)
ret = [[0 for X in range(width)] for Y in range(height)]
l = height - 1
for i in range(height):
for j in range(width):
ret[j][l] = matrix[i][j]
l -= 1
return ret
Question-8: Zero Matrix
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
Here’s the Python code:
#TODO: I will develop a better one, this looks underperforming.
def zeromatrix(matrix):
tozero = []
for ln,l in enumerate(matrix):
for cn, c in enumerate(l):
if c == 0:
tozero.append((ln,cn))
for a in tozero:
matrix = zeroify(matrix, a)
return matrix
def zeroify(matrix, place):
for ln, l in enumerate(matrix):
for cn, c in enumerate(l):
if ln == place[0] or cn == place[1]:
matrix[ln][cn] = 0
return matrix
Question-9: String Rotation
Assume you have a method isSubst ring which checks if one word is a substring of another. Given two strings, S1 and S2, write code to check if S2 is a rotation of S1 using only one call to isSubstring
Here’s the Python code:
#This was just amazing.
def is_rotation(str1, str2):
str1 += str1
if str2 not in str1: #This is our isSubstring method.
return False
return True

So, that’s all for now! If you want more of me, check my homepage for more!