Facebook Interview Question for Interns


Country: United States




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

```
def helper(digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
next_=[1]
i=0
digits=digits[::-1]
j=0
cur=0
res=[]
while(i<len(next_) or j<len(digits)):
first = next_[i] if i<len(next_) else 0
i+=1
second = digits[j] if j<len(digits) else 0
j+=1
sum_=first+second+cur
res.append(sum_%10)
cur=sum_/10
if cur:
res.append(1)
return res[::-1]
```

- mehdi January 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class NextInteger {

    public static void main(String[] args) {
        System.out.print("Next Integer: " + Arrays.toString(nextInteger(new int[]{9, 9, 8})));
    }

    private static int[] nextInteger(int[] input) {
        if (input.length == 0) {
            return input;
        }
        int sum;
        int carry = 1;
        for (int i = input.length - 1; i >= 0 && carry != 0; i--) {
            sum = input[i] + carry;
            input[i] = sum > 9 ? sum - 10 : sum;
            carry = sum > 9 ? 1 : 0;
        }
        if (carry == 0) {
            return input;
        }
        int[] result = new int[input.length + 1];
        result[0] = carry;
        System.arraycopy(input, 0, result, 1, input.length);
        return result;
    }
}

- duenytz January 22, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dsfmm

- Rimsha Maredia January 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def helper( digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
next_=[1]
i=0
digits=digits[::-1]
j=0
cur=0
res=[]
while(i<len(next_) or j<len(digits)):
first = next_[i] if i<len(next_) else 0
i+=1
second = digits[j] if j<len(digits) else 0
j+=1
sum_=first+second+cur
res.append(sum_%10)
cur=sum_/10
if cur:
res.append(1)
return res[::-1]

- Mehdi January 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int findNextInteger(int[] arr) {

		if (arr == null || arr.length == 0)
			return 0;
		int number = 0;
		
		// 10^2*1+10^1*2+10^0*3 = 100+20+3 = 234
		for (int i = arr.length - 1, j = 0; i >= 0 && j <= arr.length - 1; i--, j++) {
			number = number + (int) (Math.pow(10, i) * arr[j]);
		}
		return (number+1);
	}

- sanjos January 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

u shud return array, not integer

- VB January 26, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def nextNum(nums):
i = len(nums)
inc = 1
for a in range(i, 0, -1):
num = nums[a-1]+inc
inc = num/10
nums[a-1] = num%10
return nums

- Gordon January 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{var interger = [1,9,9]
let c = interger.count-1
var carryforward = 0
for n in 0...c{
var temp = interger[c-n]
temp = temp + 1
if temp == 10{
carryforward = 1
temp = 0
interger[c-n] = temp
}else{
carryforward = 0
interger[c-n] = temp
break
}
}
if carryforward == 1{
interger.insert(1, at: 0)
}
print(interger)
}

- Anonymous January 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var NextInteger = function (arr) {
var carry = 0;
var result = [];
for (var i = arr.length - 1; i >= 0; i--) {
var sum = 0
var addNum = 0;
if (i == arr.length - 1) {
addNum = arr[i] + 1;
} else {
addNum = arr[i];
}
sum = carry + addNum;
if (sum > 9) {
carry = 1; sum = 0;
}
else {
carry = 0;
}
result.unshift(sum);
}

if (carry > 0)
result.unshift(carry);

}

- Narendra Patel January 21, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var arr = [1, 2, 3, 4, 5];
var number = 0;

for (var i = 0; i < arr.length; i++) {
	var power = arr.length - 1 - i;
	var multiplier = 10 ** power;
	number += arr[i] * multiplier;
}

console.log(number + 1);

- ik January 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var arr = [1, 2, 3, 4, 5];
var number = 0;

for (var i = 0; i < arr.length; i++) {
	var power = arr.length - 1 - i;
	var multiplier = 10 ** power;
	number += arr[i] * multiplier;
}

console.log(number + 1);

- ik2 January 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <algorithm>

void increment_num(std::vector<int>& v) {
 
	std::reverse(v.begin(), v.end());
  v[0] += 1; 
	if (v[0] == 10) {
		v[0] = 0;
		int carry = 1;
		for (int i=1; i<v.size();++i){
			v[i] += carry;
			carry = 0;
		  if (v[i] == 10){
				v[i] = 0;
				carry = 1;
			}	
		}
    if (carry){
			v.emplace_back(carry);
		}
  }

	std::reverse(v.begin(), v.end());
}

int main(){


	std::vector<int> v{9, 9, 9};

	increment_num(v);
	for (auto a: v) {
		std::cout << a << " ";
	}
	std::cout << std::endl;

	return EXIT_SUCCESS;
}

- Anonymous January 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x = [1, 2, 3]
x = [str(i) for i in x]
x = int(''.join(x)) + 1
x = [int(i) for i in str(x)]
print(x)

- Anonymous January 28, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*given an array representing a non-negative integer (ex: 123 represented as [1,2,3]), return the next integer (output: [1,2,4])
.run through all edge cases (ex: [9,9,9,9,9,9,9,9] etc)
*/
public class IncreasingArray {

public static void main(String args[])
{
int[] input=new int[]{9,9,9,9,9};

System.out.println("Next is ==>"+Arrays.toString(getNext(input)));

}


public static int[] getNext(int[] inputArr)
{

String inputArrStr = Arrays.toString(inputArr).replaceAll("\\[|\\]|,|\\s","");
int number = Integer.parseInt(inputArrStr);
number = number + 1;
String newNumber = String.valueOf(number);
char[] newNumberArr = newNumber.toCharArray();

int[] result = new int[newNumber.length()];

for(int i=0;i<=newNumber.length()-1;i++)
{
result[i]=Character.getNumericValue(newNumberArr[i]);
}

return result;

}

}

- Praveen January 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class IncreasingArray {

    public static void main(String args[])
    {
        int[] input=new int[]{9,9,9,9,9};
        System.out.println("Next is ==>"+Arrays.toString(getNext(input)));
    }


    public static int[] getNext(int[] inputArr)
    {
        String inputArrStr = Arrays.toString(inputArr).replaceAll("\\[|\\]|,|\\s","");

        int number = Integer.parseInt(inputArrStr);
        number = number + 1;
        String newNumber = String.valueOf(number);
        char[] newNumberArr = newNumber.toCharArray();

        int[] result = new int[newNumber.length()];

        for(int i=0;i<=newNumber.length()-1;i++)
        {
            result[i]=Character.getNumericValue(newNumberArr[i]);
        }

        return result;

    }
}

- Praveen January 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] GetNextArray(int[] input) 
        {
            var value = string.Join(string.Empty, input.Select(p => p.ToString()).ToArray());

            var str = (int.Parse(value) + 1).ToString();

            var output = new int[str.Length];

            for (var index = 0; index < str.Length; index++)
            {
                output[index] = int.Parse(str[index].ToString());
            }

            return output;
        }

- Elena January 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function nextInteger(A) {
  let cur = 1

  for (let i = A.length - 1; i >= 0; i -= 1) {
    let num = cur + A[i]
    let rem = num % 10
    cur = Math.floor(num / 10)
    A[i] = rem
  }

  if (cur > 0) return [cur].concat(A)
  return A
}

- 0x March 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function sayHello(A) {
  let sp  = A.join("");
  let num = parseInt(sp)+1;
  let str = num.toString();
  let arr = str.split("");
  for(let i = 0; i < arr.length; i++) {
   arr[i] = parseInt(arr[i]);
  }
  return arr;
}

- 0z April 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def cheat( some_arr ){
  some_num = 1 + fold( some_arr, 0 ) as {  $.p*10 + $.o }
  ret_arr = list()
  while ( some_num != 0 ){
    ret_arr.add( 0 , some_num % 10 )
    some_num /= 10 
  }
  ret_arr 
}
def non_cheat( some_arr ){
  resp = rfold ( some_arr, { 'carry' : 1, 'result' : list() } ) as {
    r = ( $.o +  $.p.carry )
    $.p.carry = r / 10 
    $.p.result.add ( 0,  r % 10 )
    $.p // return 
  }
  resp.result // return 
}

println( non_cheat( [1,4,3 ] ) )

- NoOne May 04, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(parseInt(arrayOfElements.toString().replace(/,/g,'')) + 1).toString().split('');

- Fernando Castro May 10, 2019 | 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