Facebook Interview Question
Developer Program EngineersCountry: United States
Interview Type: Written Test
def laceStringsRecur(s1, s2):
"""
s1 and s2 are strings.
Returns a new str with elements of s1 and s2 interlaced,
beginning with s1. If strings are not of same length,
then the extra elements should appear at the end.
"""
def helpLaceStrings(s1, s2, out):
if s1 == '':
return out + s2
if s2 == '':
return out + s1
else:
return helpLaceStrings(s2, s1[1:], out + s1[0])
return helpLaceStrings(s1, s2, '')
print laceStringsRecur('123456789', 'abcde')
#1a2b3c4d5e6789
dummy code -
{
if s1 = ' '
- {copy s2 , from s2[(out.len/2 )] till end of s2, to out }
if s2 = ' '
- { copy s1 , from s1[(out.len/2)] till end of s1, to out }
else
{
- curr_pos = out.len / 2;
- copy char from s1[curr_pos] to out[len] , s2[curr_pos] to out[len+1]
- if (s1.len == cur_pos +1 ) s1[0] = '\0'
- if(s2.len == cur_pos +1 ) s2[0] = '\0'
- helpLaceStrings(s1, s2, out)
}
Here's one solution that aims for simplicity.
def laceStrings(s1, s2):
out = []
for i in xrange(max(len(s1), len(s2))):
out += [s1[i:i+1], s2[i:i+1]]
return ''.join(out)
If you have the itertools documentation handy, then this is more robust.
def laceStrings(*s):
return ''.join(itertools.chain.from_iterable(itertools.izip_longest(fillvalue='', *s)))
Note that there is another elegant solution called "roundrobin" in the itertools documentation.
class McNuggets(object):
def __init__(self, refqtys):
if type(refqtys) is not list:
raise Exception("Expect a list of items")
self.qtys = sorted(qtys, reverse=True)
self.result = []
def execute(self, number):
# Equation
# number = a * self.qty1 + b * self.qty2 + c * self.qty3
remainder = 0; quotient = 0
#Qtys are read in decreasing order
for qty in self.qtys:
remainder = number%qty
quotient = int(number/qty)
self.result.append(quotient)
number = remainder
return self.result
if __name__ == '__main__':
print('-'*79)
#test1
qtys = [6, 9, 20]
number = 35
print('Test input: {}'.format(qtys))
object = McNuggets(qtys)
print("Output:")
result = object.execute(number)
for inp, out in zip(qtys,result):
print("Inp: {} Out: {}".format(inp, out))
print('-'*79)
#test2
qtys = [20, 9, 6]
number = 20
print('Test input: {}'.format(qtys))
object = McNuggets(qtys)
print("Output:")
result = object.execute(number)
for inp, out in zip(qtys,result):
print("Inp: {} Out: {}".format(inp, out))
print('-'*79)
#test3
print('-'*79)
qtys = [6, 20, 9]
number = 5
print('Test input: {}'.format(qtys))
object = McNuggets(qtys)
print("Output:")
result = object.execute(number)
for inp, out in zip(qtys,result):
print("Inp: {} Out: {}".format(inp, out))
print('-'*79)
public class LaceString {
public static void main(String[] args) {
String s2 = "abcdjklmn";
String s1 = "efghi";
System.out.println("Lace String = " + laceString(s1, s2));
}
private static String laceString(String s1, String s2) {
int l1 = s1.length();
int l2 = s2.length();
StringBuffer sResult = new StringBuffer();
switch (checkBigger(l1, l2)) {
case 1:
sResult = appendString(s1, s2, l2);
sResult.append(s1.substring(l2));
break;
case -1:
sResult = appendString(s1, s2, l1);
sResult.append(s2.substring(l1));
break;
case 0:
sResult = appendString(s1, s2, l1);
break;
default:
break;
}
return sResult.toString();
}
private static StringBuffer appendString(String s1, String s2, int length) {
StringBuffer sResult = new StringBuffer();
for (int index = 0; index < length; index++) {
sResult.append(s1.charAt(index));
sResult.append(s2.charAt(index));
}
return sResult;
}
private static int checkBigger(int l1, int l2) {
if (l1 > l2) {
return 1;
} else if (l1 < l2) {
return -1;
}
return 0;
}
}
- Chad Parry November 04, 2012