Interview Question


Country: India
Interview Type: Phone Interview




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

public static int match_sticks(int[] input1, int input2, int input3) {
int sumArray []= new int[2000];
int N = input1.length;
if (input3 > N) {
return -1;
}
int combination[] = new int[input3];
int r = 0;
int index = 0;
int i=0;
while (r >= 0) {
if (index <= (N + (r - input3))) {
combination[r] = index;
if (r == input3 - 1) {
int sum=0;
for(int l=0;l<combination.length;l++){
sum=sum+input1[combination[l]];

}
if(sum%input2==0){
sumArray[i]=sum;
i++;
}

index++;
} else {
index = combination[r] + 1;
r++;
}
} else {
r--;
if (r > 0)
index = combination[r] + 1;
else
index = combination[0] + 1;
}

}



int finalSum=sumArray[0];
if(finalSum==0){
finalSum=-1;
}else{
for(int j=1;j<sumArray.length;j++){
if(sumArray[j]==0){
break;
}
if(finalSum>sumArray[j]){
finalSum=sumArray[j];
}
}
}
return finalSum;
}

- Neha April 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int match_sticks(int[] input1, int input2, int input3) {
		int sumArray []= new int[2000];
		int N = input1.length;
		if (input3 > N) {
			return -1;
		}
		int combination[] = new int[input3];
		int r = 0;
		int index = 0;
		int i=0;
		while (r >= 0) {
			if (index <= (N + (r - input3))) {
				combination[r] = index;
				if (r == input3 - 1) {
					int sum=0;
					for(int l=0;l<combination.length;l++){
						sum=sum+input1[combination[l]];
						
					}
					if(sum%input2==0){
						sumArray[i]=sum;
						i++;
					}
					
					index++;
				} else {
					index = combination[r] + 1;
					r++;
				}
			} else {
				r--;
				if (r > 0)
					index = combination[r] + 1;
				else
					index = combination[0] + 1;
			}
			
		}
		
		
		
		int finalSum=sumArray[0];
		if(finalSum==0){
			finalSum=-1;
		}else{
			for(int j=1;j<sumArray.length;j++){
				if(sumArray[j]==0){
					break;
				}
				if(finalSum>sumArray[j]){
					finalSum=sumArray[j];
				}
			}
		}
		return finalSum;
	}

- Neha April 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please explain the logic of your code and/or add comments to it for clarity?

- Killedsteel May 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this code is not working

- Yogesh May 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int match_sticks(int[] input1, int input2, int input3) {
int sumArray []= new int[2000];
int N = input1.length;
if (input3 > N) {
return -1;
}
int combination[] = new int[input3];
int r = 0;
int index = 0;
int i=0;
while (r >= 0) {
if (index <= (N + (r - input3))) {
combination[r] = index;
if (r == input3 - 1) {
int sum=0;
for(int l=0;l<combination.length;l++){
sum=sum+input1[combination[l]];

}
if(sum%input2==0){
sumArray[i]=sum;
i++;
}

index++;
} else {
index = combination[r] + 1;
r++;
}
} else {
r--;
if (r > 0)
index = combination[r] + 1;
else
index = combination[0] + 1;
}

}



int finalSum=sumArray[0];
if(finalSum==0){
finalSum=-1;
}else{
for(int j=1;j<sumArray.length;j++){
if(sumArray[j]==0){
break;
}
if(finalSum>sumArray[j]){
finalSum=sumArray[j];
}
}
}
return finalSum;
}

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

this code helped me a lot, thank you

- raji.yemineni January 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how to code using functions in c the same logic

- princy February 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anyone explain the logic of this algrthm

- javeria011 February 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anyone explain the logic of this algrthm

- javeria011 February 19, 2017 | 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