Pinterest Interview Question for Software Developers

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````def addBinary(a: str, b: str) -> str:
from collections import deque
res =  deque() # we use deque to add elements to the left

max_len = max(len(a), len(b))
a = a.zfill(max_len) # add 0s to the left side to make both strings of the same length
b = b.zfill(max_len)
# print(a, b)
carry = 0
for x, y in zip(reversed(a), reversed(b)):
carry += int(x) + int(y)
# print(x, y, curr_sum)
if carry < 2:
res.appendleft(str(carry))
else: # curr_sum >= 2
res.appendleft(str(carry%2))
carry = carry // 2 # how many 2s we have in carry

if carry == 1:
res.appendleft(str(carry)) # add the number of 2s remained in carry
return ('').join(res)``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.