Skip to content

Instantly share code, notes, and snippets.

@christopher-wong
Created July 16, 2018 23:43
Show Gist options
  • Save christopher-wong/90d7e28ee2aff0db1f5d2daba6facd52 to your computer and use it in GitHub Desktop.
Save christopher-wong/90d7e28ee2aff0db1f5d2daba6facd52 to your computer and use it in GitHub Desktop.
Facebook New Grade SWE Interview
# Welcome to Facebook!
# This is just a simple shared plaintext pad, with no execution capabilities.
# When you know what language you'd like to use for your interview,
# simply choose it from the dropdown in the top bar.
# Enjoy your interview!
## Move all nonzero integers to left of an array and return the number of nonzero integers
[0, -1, 0, 3, 0]
[-1, 3, 0, 0, 0]
[3, -1, 0, 0, 0]
return 2
##
def endZeroes(arr):
length = len(arr)
count = 0
for i in range(length):
if arr[i] != 0:
arr[count] = arr[i]
count += 1
non_zero_count = count
## we have copied all non zero elements to front of array
while count < length:
arr[count] = 0
count += 1
return non_zero_count - count
first = 0
last = length - 1
if arr[i] == 0:
arr[last -= 1] = arr[i]
else:
arr[first += 1] = arr[i]
arr = [-1, 3, 0, 0, 0]
# length: 5
# count: 2
# i: 4
## Lexographic compare between 2 strings with consecutive digits treated as a single element, return the "smaller" string (if equal, return either string)
"a9", ""
"a9", "a11"
normal alphabetical comparison:
a9
a11 <<<<
modified alphabetical comparison:
a 9 <<<<
a 11
[a-z0-9]
letters < numbers
a < z < 0 < 9 < ...
a007 == a7
'''
[a, 9]
[a, 1, ,1]
'''
def squashDigits(arr):
new_arr = []
for i in len(arr):
if !arr[i].isDigit():
new_arr.append(item)
else:
current_num = ""
current_num += arr[i]
while i < len(arr) and new_arr[i + 1].isDigit():
current_num += new_arr[i + 1]
new_arr.append(int(current_num))
return new_arr
# ["a", "9"]
# ["a", "11"]
# "009" vs. "11"
def stringStringCompare(a, b):
if a is None or b is None:
return -1
if a == "":
return a:
elif b == "":
return b
else:
# guarentee a is longer string
if len(a) < len(b): # a = "1111111" b = "a1" len(a) > len(b) - len(arr_a) = 1, len(arr_b) = 2
a, b = b, a
arr_a = squashDigits(a.split())
arr_b = squashDigits(b.split())
for i in range(arr_a): # << longer array
first = arr_a[i]
second = arr_b[i]
# both are ints
if first.isdigit() && second.isdigit():
if first < second:
return a
## both are strings
elif first.isalpha() && second.isalpha():
if first < second:
return a
## first is string
elif first.isalpha() && second.isdigit():
# assume we don't know how comp works between different types
return a
## second is string
elif first.isdigit() && second.isalpha():
return b
# the same strings
return b
# ["a", 9]
# ["a", 9, "b"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment