## Linkedin Interview Question for Member Technical Staffs

Team: Tools team
Country: United States
Interview Type: In-Person

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

Simply iterate from 1 through n and n % i == 0, print the number. Elegant one liner in python below:

``````def getAllFactors(n):
return [i for i in range(1, n+1) if n % i == 0]``````

Test code:

``print(getAllFactors(12)) # Prints [1, 2, 3, 4, 6, 12]``

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

@prudent_programmer, Good, short and clean solution.

2 improvements can be done on top of this:
1. don't iterate 1 to n, iterate thru 1 to sqrt(n) and add both i and n//i
2. if an odd number go 2 by 2 instead of 1 by 1

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

Awesome suggestions! Thank you!

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

``````public static List<Integer> factors(int num){
List<Integer> result = new ArrayList<>();

if(num == 0)
return result;

boolean isOdd = num % 2 != 0;
double limit = Math.floor(Math.sqrt(num));
int i = 1;

while(i <= limit){
if(num % i == 0){
}

i++;

if(isOdd)
i++;
}

return result;
}
public static void main(String[] args) {
System.out.println("Factors of Number: " + factors(13));

}``````

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

Your solution doesn't account for an edge case where i == num / i. i.e., for numbers such as 25 or 49.

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.