Facebook Interview Question for Developer Program Engineers


Country: United States
Interview Type: Written Test




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

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(s1[1:], s2[1:], out + s1[0] + s2[0])
        return helpLaceStrings(s1, s2, '')

- Chad Parry November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

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

- Anonymous November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)
}

- Anonymous November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Chad Parry November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Similar to merging of two arrays, but we dont need compare.

- Andi November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Mis Nisha Kothari you are violating Honor Code

- Ali November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this'll work:

def laceStringsRecur(s1, s2):
    
    output = ""
    maxrange = min(len(s1), len(s2))
    for i in range(maxrange):
        output += s1[i] + s2[i]
    
    output += s1[maxrange:]
    output += s2[maxrange:]
    
    return output

- wick3dsunny November 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- sujayyendhiren January 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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;
	}
}

- Venkat November 03, 2012 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More