Coding Interview | Easy Difficulty Flashcards
isPalindrome
Write a function that takes in a non-empty string and returns a boolean representing whether the string is a palindrome, via slice that steps backwards.
def isPalindrome(string):
if string == string[::-1]:
return True
else:
return False
isPalindrome
Write a function that takes in a non-empty string and returns a boolean representing whether the string is a palindrome, use the join built-in function.
def isPalindrome(string):
if string == “”.join(reversed(string)):
return True
else:
return False
isPalindrome
Write a function that takes in a non-empty string and returns a boolean representing whether the string is a palindrome, use a while loop
def isPalindrome(string):
leftIdx = 0
rightIdx = len(string) - 1
while leftIdx < rightIdx:
if string[leftIdx] != string[rightIdx]:
return False
leftIdx += 1
rightIdx -= 1
return True
isPalindrome
Write a function that takes in a non-empty string and returns a boolean representing whether the string is a palindrome, via a new array.
def isPalindrome(string):
reversedChars = []
for i in reversed(range(len(string))):
reversedChars.append(string[i])
return string == ““.join(reversedChars)
isPalindrome
Write a function that takes in a non-empty string and returns a boolean representing whether the string is a palindrome, use recursion to solve the problem.
this is not pep8 def isPalindrome(string, i = 0): j = len(string) - 1 - i return True if i \>= j else string[i] == string[j] and **isPalindrome(string, i + 1)**
True or False
It is considered a best practice to NEVER return null when returning a collection or enumerable. (ie twoNumbersSum)
True
Always return an empty enumerable/collection!
return []
twoNumberSum
Write a func that takes in a non-empty arr of distinct integers and an integer representing a target sum. If any two number in the input sum up to the target sum, the func returns them in an arr. If no two equates to the target sum return an empty arr, use brute-force (nested for loop).
def twoNumberSum(array, targetSum): **for** i in range(len(array)): **for** j in range(i + 1, len(array)): if array[i] + array[j] == targetSum: return array[i], array[j] return []
twoNumberSum
Write a func that takes in a non-empty arr of distinct integers and an integer representing a target sum. If any two number in the input sum up to the target sum, the func returns them in an arr. If no two equates to the traget sum return an empty arr, use a hash table.
def twoNumbersSum(arr, targetSum):
nums = {}
for num in arr:
potentialMatch = targetSum - num
if potentialMatch in nums:
return [potentialMatch, num]
else:
nums[num] = True
return []
twoNumberSum
Write a func that takes in a non-empty arr of distinct integers and an integer representing a target sum. If any two number in the input sum up to the target sum, the func returns them in an arr. If no two equates to the target sum return an empty arr, use a while loop.
def twoNumbersSum(arr, targetSum):
arr.sort()
left = 0
right = len(arr) -1
while left < right:
currentSum = arr[left] + arr[right]
if currentSum == targetSum:
return arr[left], arr[right]
elif currentSum < targetSum:
left += 1
elif currentSum > targetSum:
right -= 1
return []
firstNonRepeatingCharacter
Write a func that takes in a str of lowercase English-alpha letters and returns the index of the strs first non-repeating char, if the input str doesn’t have any non-repeating chars return -1, use the python built-in Counter method.
from collections import Counter
def firstNonRepeatingCharacter(string): freq = **Counter**(string)
for idx in range(len(string)):
char = string[idx]
if(freq[char] == 1):
return idx
return -1
firstNonRepeatingCharacter
Write a func that takes in a str of lowercase English-alpha letters and returns the index of the strs first non-repeating char, if the input str doesn’t have any non-repeating chars return -1, use a hash table (python dict).
def firstNonRepeatingCharacter(string): **characterFreq = {}**
for char in string:
characterFreq[char] = characterFreq.get(char, 0) + 1
for idx in range(len(string)):
char = string[idx]
if characterFreq[char] == 1:
return idx
return -1
firstNonRepeatingCharacter
Write a func that takes in a str of lowercase English-alpha letters and returns the index of the strs first non-repeating char, if the input str doesn’t have any non-repeating chars return -1, use brute force (nested for loop)
def firstNonRepeatingCharacter(string): **for** i in range(len(string)): duplicate = False **for** j in range(len(string)): if string[i] == string[j] and i != j: duplicate = True if not duplicate: return i return -1
bubbleSort
Write a func that takes in an array of integers and returns a sorted version of that array. Use the bubble sort algorithm, nested for loop.
def bubbleSort(array): n = len(array) **for** i in range(n-1): **for** j in range(0, n-i-1): if array[j] \> array[j+1]: array[j], array[j+1] = array[j+1], array[j] return array
bubbleSort
Write a func that takes in an array of integers and returns a sorted version of that array. Use the bubble sort algorithm, do a while loop amd use a separate func to complete the swap
def bubbleSort(array):
isSorted = False
counter = 0
while not isSorted:
isSorted = True
for i in range(len(array) -1 - counter):
if array[i] > array[i + 1]:
swap(i, i + 1, array)
isSorted = False
counter += 1
return array
def swap(i, j, array): array[i], array[j] = array[j], array[i]
insertionSort
Write a func that takes in an arr of int and returns a sorted version of that arr, use a while loop.
def insertionSort(array): for i in range(len(array)): key = array[i] j= i - 1 **while** j \>= 0 and key \< array[j]: array[j + 1] = array[j] j -= 1 array[j + 1] = key
return array
selectionSort
Write a func that takes in an arr of int and returns a sorted version of that arr, use the selection sort algo with a nested for loop
def selectionSort(array): **for** i in range(0, len(array)): iMin = i **for** j in range(i + 1, len(array)): if array[iMin] \> array[j]: iMin = j if i != iMin: array[i], array[iMin] = array[iMin], array[i]
return array