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]

- prudent_programmer March 17, 2018 | Flag Reply
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

- assertNull April 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Awesome suggestions! Thank you!

- prudent_programmer April 17, 2018 | Flag Reply
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){
				result.add(i);
				result.add(num / i);
			}
			
			i++;
			
			if(isOdd)
				i++;
		}
		
		return result;
	}
	public static void main(String[] args) {
		System.out.println("Factors of Number: " + factors(13));

	}

- AJ July 28, 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