This will be a document of all the coding questions I experience during coding interviews!
If you have any questions or want further explanation, please reach out to me on Instagram at: @thecodingempire.
There will only be letters in the string (no numbers of punctuation symbols)
def sortLetters(strParam):
if strParam == "": #base case call
return strParam
strAns = strParam[0] #starts off strAns with something
for i in range(1, len(strParam)): #loops for all unaccounted letters of strParam
strAns = putLetter(strAns, strParam[i])
return strAns
def putLetter(strAns, letter):
idx = len(strAns)-1 #starts the loop from the end of the string
while (idx >= 0):
if (strAns[idx] < letter): #checks to see if this is the spot to insert letter
return strAns[0:idx+1] + letter + strAns[idx+1:] #inserts letter
idx -= 1
return letter + strAns #letter is less than all letters in word already, add to front
sortLetters(input()) #calls the function with parameters entered by the user
Each time will be formatted as “:__am/pm-:_am/pm”
def timeCalculate(strParam):
strLst = strParam.split(":") #creates a list of size 2 with the times needed
beginTime, endTime = strLst #sets the begin and end times
minBeginTime = convertToNum(beginTime)
minEndTime = convertToNum(endTime)
if minEndTime < minBeginTime: #goes into next day, must calculate the amount of time
#left today and the amount of time into tomorrow
return 24*60 - minEndTime + minBegimTime
return minEndTime - minBeginTime #otherwise, simple calculation based on military time
def convertToNum(time):
colonIdx = time.find(":") #hard-coded the determination of hours and minutes in time
hours = int(time[:colonIdx])
minutes = int(time[colonIdx+1:len(time)-2])
if "pm" in time:
if hours < 12:
hours += 12 #using military time for easier handling of difference
return 60 * hours + minutes #converts the time into minutes past midnight
timeCalculate(input()) #calls the function with parameters entered by the user
Given a number, n, sum up all multiples of 3 and 5 that occur [0, n).
def customSort(num):
multiples = set()
for i in range(num): #loop in range [0, *n*)
if i % 3 == 0:
if i not in mulitples: #checks to see if multiple was already included
multiples.add(i) #adds if not in set
elif i % 5 == 0:
if i not in multiples: #this can be optimized more because sets will not
multiples.add(i) #have multiple instances of a number (no need for
#if statment checking if number is in set).
multiples = [*multiples] #converts the set into a list for sum calculation
return sum(multiples) #uses built-in sum method to calculate sum
print(customSort(input())) #calls the method and prints answer
Here’s the roadmap I would follow if I had to start programming all over again