# Let's start with a sample problem: compute the product of all elements in list L
# (L[0]*L[1]*L[2]*...)
L = [1, 2, 3]
prod = 1
for elem in L:
prod *= elem
print(prod)
Best coding practices:
# Let's look at efficiency - given some list of numbers L, compute the average of the numbers
# in the list. Then append a number to the list and recompute the average.
L = [0]*100000000
#L.append(new_number) <- appending a number to the end of the list
s = 0
for elem in L:
s += elem
avg = s / len(L)
print(avg)
#append a number and compute the average
new_num = 10
L.append(new_num)
s = 0
for elem in L:
s += elem
avg = s / len(L)
print(avg)
#11.5 seconds
L = [0]*100000000
#L.append(new_number) <- appending a number to the end of the list
s = 0
for elem in L:
s += elem
avg = s / len(L)
print(avg)
#append a number and compute the average
new_num = 10
L.append(new_num)
s += new_num
avg = s/len(L)
print(avg)
# this code is more efficient because it does half as much the computation
#6.76 seconds
Now that you know all of the best technical practices for a coding interview, let's talk about behavioural practices and what to expect.
Usually, you will be asked to solve 2 problems in 40 minutes.
Typically, for a "software engineering" job, you would have 1 conversation with a recruiter talking about yourself and 2 coding interviews (each 40 minutes with 2 problems)
# Problem to try: Given an array nums of size n, print the majority element. The majority element
# is the element that appears more than n/2 times. Assume there is a majority element in nums.
nums = [1, 2, 2, 10, 0, 2, 2]
n = len(nums)
dict = {}
for elem in nums:
if elem not in dict:
dict[elem] = 1
else:
dict[elem] += 1
for key in dict:
if dict[key] > n/2:
print(key)
n = [1, 2, 2, 10, 0, 2, 2]
store = (len(n)/2)
element = "none"
for num in n:
if n.count(num) > store:
store = n.count(num)
element = num
print(element)
nums = [1, 2, 2, 10, 0, 2, 2]
n = len(nums)
for elem in nums:
e = nums.count(elem)
if e > n/2:
maj_elem = elem
print (maj_elem)
# Given a string s consisting of words and spaces, print the length of the last word in the string.
# A word is a substring consisting of non-space characters only. No punctuation and no numbers
# some examples
s = "a great big world"
s = "a"
s = " "
s= "one"
s = "a great big world "
#two ways to loop from the end of an array
for i in range(len(s)):
print(s[-1-i])
for i in range(len(s)):
print(s[len(s) - i - 1])
s = ""
s = s.strip()
space = s.rfind(" ")
last = s[space+1:]
print(len(last))
# problem to try at home: given a natural number int, write a function that outputs true if
# the number is a palindrome or false if the number is not a palidrome.