Facebook Interview Question for Java Developers


Country: United States




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

We can ignore the input and and re-populate the array using division and modulo by (array.length/nLetters).
If we don't know how many letters (#{a,b,c} = 3), we can first scan the input to find that out.

- Hagai C February 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

nknkl;khjkh;

- Anonymous February 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String[] staggerArrays(String[] input) {
		String[] output = new String[input.length];

		int j = 1;
		for (int i = 0; i < input.length; i = i + 3) {
			
			// The one in comments work too
			// output[3*(j-1)] = input[i];
			// output[3*(j-1) + 1] = input[i+1];
			// output[3*(j-1) + 2] = input[i+2];

			output[i] = "a" + j;
			output[i + 1] = "b" + j;
			output[i + 2] = "c" + j;

			j++;
		}

		return output;

	}

- Anonymous February 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String[] staggerArrays(String[] input) {
		String[] output = new String[input.length];

		int j = 1;
		for (int i = 0; i < input.length; i = i + 3) {
			
			// The one in comments work too
			// output[3*(j-1)] = input[i];
			// output[3*(j-1) + 1] = input[i+1];
			// output[3*(j-1) + 2] = input[i+2];

			output[i] = "a" + j;
			output[i + 1] = "b" + j;
			output[i + 2] = "c" + j;

			j++;
		}

		return output;

	}

- Itika February 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;

public class StaggerArrays {
    public static void main(String[] args) {

        String array1[] = new String[]{"a1", "a2", "a3","a4"};
        String array2[] = new String[]{"b1", "b2", "b3","b4"};
        String array3[] = new String[]{"c1", "c2", "c3","b4"};

        String[] staggerArray = new String[array1.length * 3];

        for (int i=0,j =0;i < staggerArray.length; i+=3,j++) {
                staggerArray[i] = array1[j];
                staggerArray[i + 1] = array2[j];
                staggerArray[i + 2] = array3[j];
        }
        System.out.println(Arrays.asList(staggerArray));
    }

}

- {novice} February 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a1a2|a3a4 b1b2|b3b4 c1c2|c3c4
a1a2|b1b2|a3a4|b3b4|c1c2c3c4
a1a2|b1b2|a3a4|c1c2|b3b4c3c4
(a1a2b1b2c1c2)(a3a4b3b4c3c4)

- sumitgaur.iiita February 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

resa = []
resb = []
resc = []
for indexi, i in enumerate(lista[::3]):
resa.append(i)
resb.append(lista[index+1])
resc.append(lista[index+2])

- Anonymous February 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//var strArray = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3", "d1", "d2", "d3"]
var strArray = ["a1", "a2", "b1", "b2", "c1", "c2"]

func firstLetterOf(_ string: String) -> Character? {
    return string.first
}

func sortArray(_ array: inout [String]) -> [String] {
    guard !array.isEmpty,
          let firstLetter = firstLetterOf(array[0]) else {
        return array
    }
    
    // calculate number of letters in one group
    var numberOfLettersInGroup = 0
    for group in array {
        guard let letter = firstLetterOf(group), letter == firstLetter else {
            break
        }
        numberOfLettersInGroup += 1
    }
    
    // sorting algorithm
    let numberOfGroups = array.count / numberOfLettersInGroup
    var currentIndex = 0
    var p = 0
    
    for i in 0..<numberOfLettersInGroup-1 {
        let multiplicator = numberOfLettersInGroup - i
        
        for j in 0..<numberOfGroups {
            let removingIndex = j * multiplicator + p
            
            // 
            print("removing from: \(removingIndex)")
            let element = array.remove(at: removingIndex)
            
            //
            print("inserting to: \(currentIndex)")
            array.insert(element, at: currentIndex)
            
            //
            currentIndex += 1
            print(array)
        }
        p = currentIndex
    }
    
    return array
}

sortArray(&strArray)

- Igor Novik February 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Solution {
  public static void main(String[] args) {
    int n = 5;
    String [] outputArray= new String[n*3];
    for (int i = 0; i < n*3; i++) {
      int j= (i / 3) + 1;
      outputArray[i] ="a"+j;
      outputArray[++i] ="b"+j;
      outputArray[++i] ="c"+j;
    }
    
    System.out.println(Arrays.asList(outputArray));
  }
}

- Anonymous February 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//considering we were give the value of N and array of size 3N.
//input ['a1,'a2,'a3','b1','b2','b3','c1','c2','c3'], N= 3
//output ['a1,’b1,’c1,’a2’,’b2’,’c2’,’a3’,’b3’,’c3']
public String[] getStaggeredArray(String[] arry, int n) {
String[] result = new String[arry.length];
if ( (3*n) != arry.length) {
System.out.println("Something is wrong");
}
int counter = 0;
for(int i =0; i < n; i++) {
result[counter++] = arry[i];
result[counter++] = arry[(i+n)];
result[counter++] = arry[(i+(2*n))];
}
return result;
}
// Both time complexity and space complexity O(n)

- sgundumonu February 15, 2018 | 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