## Facebook Interview Question for Software Engineer Interns

Country: United States
Interview Type: Phone Interview

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

Some explanations in the comment to implementation.
I didn't find similar solutions in the example from above, so, I've decided to add my own.

``````/**
* Find all the numbers the sum of cube of each digits is the number itself.
*
* @implNote the first thing that should be done is how to identify upper boundary for list of
* numbers.
*
* If length == 1 (0 <= n < 9), then the max value of the sum of cubes will be 9*9*9 = 729
*
* If length == 2 (10 <= n < 100), then the max value of the sum of cubes will be 9*9*9*2 = 1458
*
* If length == 3 (100 <= n < 1000), then the max value of the sum of cubes will be 9*9*9*3 = 2187
*
* If length == 4 (1000 <= n < 10000), then the max value of the sum of cubes will be 9*9*9*4 =
* 2916
*
* If length == 5 (10000 <= n < 100000), then the max value of the sum of cubes will be 9*9*9*5 =
* 3645
*
* It means that for numbers with length equals to 4 there is a probability that it could be a
* number with sum of cubes equals to that number, but for numbers with length that equals to 5,
* it is not the case, expected sum will be less than a number all the time. So, we can use 10000 as
* a upper boundary for the calculation.
*/
public class SumOfCubes {

public static void main(String[] args) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < 10000; i++) {
if (getSumOfCubes(i) == i) {
output.append(i).append(System.lineSeparator());
}
}
System.out.println(output);
}

private static int getSumOfCubes(final int number) {
if (number == 0) {
return 0;
}
int lastDigit = number % 10;
return getSumOfCubes(number / 10) + lastDigit * lastDigit * lastDigit;
}
}``````

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

Using ruby

``````def cube_all_digit(input)
retMe = 0
input.to_s.each_char do |digit|
retMe += digit.to_i**3
end
return retMe
end``````

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

``````def cube_all_digit(input)
retMe = 0
input.to_s.each_char do |digit|
retMe += digit.to_i**3
end
return retMe
end``````

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

``````public static void FindNarcisisitcNumbers() {
int n = 3;
List<Integer> nums = new ArrayList<Integer>();
int power = 1;
for (int i = 1; i <= n; i++) {
for (int j = power; j < power * 10 ; j++) {
int temp = j;
int sum = 0;
while (temp != 0) {
sum += Math.pow(temp % 10, i);
temp /= 10;
}
if (sum == j)
}
power *= 10;
}
for (int nu : nums)
System.out.println(nu);
}``````

Result:
0
1
2
3
4
5
6
7
8
9
153
370
371
407

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

def cube_digit(n):

if n == 0:
return 0
return (n % 10)**3 + cube_digit(n//10)

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

Hope this helps

``````public static void main(String[] args) {
System.out.print("Enter the limit : ");
int n = (new Scanner(System.in)).nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i == findSOC(i)) {
}
}
System.out.println(list);
}

private static int findSOC(int i) {
if (i == 0)
return 0;
return findSOC(i / 10) + cube(i % 10);
}

private static int cube(int i) {
return i * i * i;
}``````

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

#include<stdio.h>
main()
{
int num,,sum=0,i;
scanf("%d",&num);
for(i=num;i!=0;i/=10)
sum+=pow(i%10,3);
if(sum==num)
return(1);
else
return(0);
}

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

``````func cubeAllDigits(in number:Int) -> Int {
var sum = 0
var left = number
while left > 0 {
let x = left % 10
sum += x * x * x
left = (left - x) / 10
}
return sum
}

print(cubeAllDigits(in: 2)) // 8
print(cubeAllDigits(in: 153)) // 153
print(cubeAllDigits(in: 103)) // 28
print(cubeAllDigits(in: 555555)) // 750``````

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

{ static void Main(string[] args)
{
double res;

for (int i = 1; i < 10; i++)
{
for (int j = 1; j < 10; j++)
{
for (int k = 1; k < 10; k++)
{
res = Math.Pow(i, 3) + Math.Pow(j, 3) + Math.Pow(k, 3);

if ( res == int.Parse(i.ToString() + j.ToString() + k.ToString()) )
{
Console.WriteLine(res);
}
}
}
}
}}

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

``````public static int GetCubeOfEachDigit(int input)
{
var str = input.ToString();

var value = str.Select(d => Math.Pow(int.Parse(d.ToString()), 3));

return (int)value.Sum();
}``````

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.