Cloudera Interview Question for Software Engineer / Developers


Country: United States




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

static void printDivisors(int n){
		System.out.println("1*" + n);
		printDivisors("",1,n);
	}
	
	static void printDivisors(String prefix,int prev,int n){
		if(n<2)
			return;
		int s = (int) Math.ceil(Math.sqrt(n));
		for(int i = 2;i<=s;i++){
			if(n%i == 0){
				if(i>=prev && n/i>=i){
					System.out.println(prefix + i + "*" + n/i);
					printDivisors(prefix + i + "*",i,n/i);
				}
			}
				
		}
	}

- loveCoding January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you are including 1 then 1*2*6 can also be one solution..

- loveCoding January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

include 1 if it is first time call.
1*1*2*

- NJ January 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 4 vote

for(int i = 2;i<s;i++) should be for(int i = 2;i<=s;i++)

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

Find all the prime factors of the number.
We can express the number as product of its prime factors.

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

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

int main() {
double num;
cin >> num;
for(int i=1;i<=sqrt(num);i++) {
if((int)num%i==0)
cout << i << " * " << num/i << endl;

}
getch();
return 0;
}

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

What about this one? Let me know if I have made any mistakes

public static void printDivisors(int n){
		int m=0;
		HashMap<Integer, Integer> ab=new HashMap<Integer, Integer>();
		while(m<n/2){
		    m++;
		    if(n%m==0){
		    	if(!ab.containsKey(n/m)){
		    		ab.put(m, n/m);
		    	}
		    }
		    	
		}
		Iterator x=ab.entrySet().iterator();
		while(x.hasNext()){
			Map.Entry numbers=(Map.Entry)x.next();
			System.out.println(numbers.getKey());
			System.out.println(numbers.getValue());
			
		}
	}

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

import java.util.*;
import java.lang.*;

class Main{

	public static void main (String[] args){
		printFactors(12);
	}

	public static void printFactors(int n){
		for(int i = 1; i <= Math.sqrt(n); i++){
			if(n % i == 0){
				System.out.println(""+(n/i)+"*"+i);
			}
		}
	}
}

- Anonymous August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I know I am excluding the 1*n case, but that is trivial

vector < string> print_all_factorizations(int number)
{
	vector<string> result ;
	int j =number;
	for(int i =1; i < j; i++)
	{
		if(number % i == 0)
		{
			j=number/i;
			//cout << i << "*" << j << endl;
			char buff[512];
			sprintf(buff, "%i*%i",i,j);
			result.push_back(buff);
			
			if(j != number)
			{
				vector<string> subcall = print_all_factorizations(j);
				for(int s =0; s < subcall.length; s++)
				{
					sprintf(buff, "%i*%i",i,subcall[s].c_str());
					result.push_back(buff);

				}
			}
		}
	}
	return result;
}

- sscompbio June 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry, small correction

vector < string> print_all_factorizations(int number)
{
	vector<string> result ;
	int j =number;
	for(int i =2; i < j; i++)
	{
		if(number % i == 0)
		{
			j=number/i;
			//cout << i << "*" << j << endl;
			char buff[512];
			sprintf(buff, "%d*%d",i,j);
			result.push_back(buff);
			
			if(j != number)
			{
				vector<string> subcall = print_all_factorizations(j);
				for(int s =0; s < subcall.size(); s++)
				{
					sprintf(buff, "%d*%s",i,subcall[s].c_str());
					result.push_back(buff);

				}
			}
		}
	}
	return result;
}

- sscompbio June 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry , this also prints dupes

for 12
it will print
3 * 2 * 2
2 * 2 * 3

and both are same.

- wrong November 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<vector<int>>factors(int num)
{
vector<vector<int>> result;

if(num==1)
{
result.push_back({1});
return result;
}

result.push_back({1,num});


for(int i =2; i< num;i++)
{
if(num%i==0)
{
int rest = num/i;
if(rest>=i)
{
//result.push_back(i,rest);

vector<vector<int>> restresult = factors(rest);

for(auto& item: restresult)
{
if(item[0]==1)
{
item[0]= i;
result.push_back(item);
}
else
{
if(i<= item[0])
{
item.insert(item.begin(),i);
result.push_back(item);
}

}

}


}
else
break;


}


}


}

- Xiyu December 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<vector<int>>factors(int num)
{
	vector<vector<int>> result;

	if(num==1)
	{
		result.push_back({1});
		return result;
	}

	result.push_back({1,num});


	for(int i =2; i< num;i++)
	{
		if(num%i==0)
		{
			int rest = num/i;
			if(rest>=i)
			{
				//result.push_back(i,rest);
				
				vector<vector<int>> restresult = factors(rest);

				for(auto& item: restresult)
				{
					if(item[0]==1)
					{
						item[0]= i;
						result.push_back(item);
					}
					else
					{
						if(i<= item[0])
						{
							item.insert(item.begin(),i);
							result.push_back(item);
						}

					}
					
				}


			}
			else
				break;


   		}
		

	}
	

}

- Xiyu December 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<vector<int>>factors(int num)
{
	vector<vector<int>> result;

	if(num==1)
	{
		result.push_back({1});
		return result;
	}

	result.push_back({1,num});


	for(int i =2; i< num;i++)
	{
		if(num%i==0)
		{
			int rest = num/i;
			if(rest>=i)
			{
				//result.push_back(i,rest);
				
				vector<vector<int>> restresult = factors(rest);

				for(auto& item: restresult)
				{
					if(item[0]==1)
					{
						item[0]= i;
						result.push_back(item);
					}
					else
					{
						if(i<= item[0])
						{
							item.insert(item.begin(),i);
							result.push_back(item);
						}

					}
					
				}


			}
			else
				break;


   		}
		

	}
	

}

}

- Xiyu December 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple recursive approach

void subset_product(vector<int> nums, int target, int product, vector<int> &temp, int pos) {
	if(target == product) {
		result.push_back(temp);
		return;
	}
	if(pos >= nums.size() || product > target)
		return;

	for(int i=pos; i<nums.size(); i++) {
		temp.push_back(nums[i]);
		subset_product(nums, target, product * nums[i], temp, i);
		temp.pop_back();
	}

	
}

int main() {
	
	int arr[] = {2,3,4,5,6,7,8,9,10,11,12};

	vector<int> nums (arr, arr + sizeof(arr)/sizeof(arr[0])) ;

	vector<int> temp;
	subset_product(nums, 12, 1, temp, 0);

	return 0;
}

- Anonymous January 18, 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