Interview Question


Country: United States




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

import java.io.*;
public class CandidateCode
{

private static int distributeCadbury(int input1,int input2,int input3,int input4)
{
//Write code here
int totalNumberOfStudentsDistributed=0;
for (int i = input1;i<= input2;i++)
{
for (int j= input3;j<=input4;j++)
{
System.out.println(" i * j ="+i+" * "+j+" ="+i*j);
int studDistrubuted = noOfStudentDistrubuted(i,j);
System.out.println("No of student got cadbery ="+studDistrubuted);
totalNumberOfStudentsDistributed += studDistrubuted;
}
}

return totalNumberOfStudentsDistributed;
}

private static int noOfStudentDistrubuted(int i, int j)
{
int count = 0;
// TODO Auto-generated method stub
do
{
if (i==j)
{
System.out.println("i == j returning 1 ");
i = 0;
j = 0;
count++;
}
else if (i > j)
{

i = i - j;
count++;
System.out.println("Remaining Part Size "+i+"*"+j);
}
else
{
j = j -i;
System.out.println("Remaining Part Size "+i+"*"+j);
count++;
}
}while(i!=0 && j!=0);
return count;
}
}

- Tejas Sawant May 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;
public class CandidateCode 
{ 

    private static int distributeCadbury(int input1,int input2,int input3,int input4)
	{
		//Write code here
		int totalNumberOfStudentsDistributed=0;
		for (int i = input1;i<= input2;i++)
		{
			for (int j= input3;j<=input4;j++)
			{
				System.out.println(" i * j ="+i+" * "+j+" ="+i*j);				
				int studDistrubuted = noOfStudentDistrubuted(i,j);
				System.out.println("No of student got cadbery ="+studDistrubuted);
				totalNumberOfStudentsDistributed += studDistrubuted;
			}
		}
		
		return totalNumberOfStudentsDistributed;
	}

	private static int noOfStudentDistrubuted(int i, int j)
	{
		int count = 0;
		// TODO Auto-generated method stub
		do
		{
			if (i==j)
			{
				System.out.println("i == j returning 1 ");
				i = 0;
				j = 0;
				count++;
			}
			else if (i > j)
			{
				
				i = i - j;
				count++;	
				System.out.println("Remaining Part Size "+i+"*"+j);
			}
			else
			{
				j = j -i;
				System.out.println("Remaining Part Size "+i+"*"+j);
				count++;
			}
		}while(i!=0 && j!=0);
		return count;
	}
}

- Tejas Sawant May 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if input id 6,5,4,3 then?

- sparsh ahuja April 30, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Cadbury 
{ 

	static int  finalCount=0;
    public static int distributeCadbury(int input1,int input2,int input3,int input4)
    {
        
        ArrayList<String> boxDimensions = new ArrayList<String>();
       /* boxDimensions.add(String.valueOf(input1)+"*"+String.valueOf(input2));
        boxDimensions.add(String.valueOf(input3)+"*"+String.valueOf(input4));
        boxDimensions.add(String.valueOf(input1)+"*"+String.valueOf(input4));
        boxDimensions.add(String.valueOf(input3)+"*"+String.valueOf(input2));*/
        ArrayList<String> length = new ArrayList<String>();
        ArrayList<String> breadth = new ArrayList<String>();
        length.add(String.valueOf(input1));
        length.add(String.valueOf(input2));
        breadth.add(String.valueOf(input3));
        breadth.add(String.valueOf(input4));
        
        for (String len : length) {
			for (String bre : breadth) {
				boxDimensions.add(len+"-"+bre);
			}
		}
        
        for (String boxDim : boxDimensions) {
        	System.out.println("boxDim: "+boxDim);
        	int count = 0;
        	String[] dim = boxDim.split("-");
        	if(boxDim != null){
    		Integer len = Integer.valueOf(dim[0]);
			Integer bre = Integer.valueOf(dim [1]);
        	count = calcualteCount(count, len, bre);
        	finalCount = finalCount+count;
        	}
		}
        
        System.out.println("Finalcount"+finalCount);
		return finalCount;
        
    }
    private static int calcualteCount(int count, int len,  int bre) {
 		//System.out.println(count+":"+len+":"+bre);
			if(len > bre){
 			if(len - bre >= 0 && len > 0 && bre > 0){
 				count = calcualteCount(count, (len - bre) , bre);
 				count = count+1;
 			}else{
 				return count;
 			}
 			
 		}else{
 			if(bre - len >= 0 && bre > 0 && len > 0){
 				count = calcualteCount(count, len, (bre - len));
 				count = count+1;
 			}else{
 				return count;
 			}
 			
 		}
		return count;
		
		
	}

- Anonymous May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can this problem be solved in O(1) or O(n) time?

- Rahul May 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.arjunt;

import java.io.*;

public class CandidateCode
{

    public static void main(String args[]){
        System.out.println("Total No of Students Count:"+distributeCadbury(5,6,3,4));
    }

    public static int distributeCadbury(int input1,int input2,int input3,int input4)
    {
        System.out.println("hello");
        int chocDistributionCount = 0;
        for(int i=input1;i<=input2;i++){
            for(int j=input3;j<=input4;j++){
                int studentsCount = getCountOfStudents(i,j);
                chocDistributionCount += studentsCount;
            }
        }
        return chocDistributionCount;
    }

    public static int getCountOfStudents(int length,int breadth){
        int count = 0;

        do{
            if(length > breadth){
                length = length-breadth;
                count++;
             //   System.out.println("Remaining Part when length is greater:"+length+"*"+breadth);
            }else if(length == breadth){
                length = 0;
                breadth = 0;
               // System.out.println("Remaining Part when equal:"+length+"*"+breadth);
                count++;
            }else{
                breadth = breadth-length;
                count++;
               // System.out.println("Remaining Part when length is smaller:"+length+"*"+breadth);
            }
        }while(length !=0 && breadth !=0);

        return count;
    }

}

- MrBlogger1 May 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function cadbury($input1,$input2,$input3,$input4) {
        $intChocDistributionCount = 0;
		for($i=$input1;$i<=$input2;$i++){
            for($j=$input3;$j<=$input4;$j++){
               $intStudentsCount = calculateCountOfStudents($i,$j,$intChocDistributionCount);
			   $intChocDistributionCount += $intStudentsCount;
            }
        }
        return $intChocDistributionCount-1;
    }

    function calculateCountOfStudents($intLength,$intBreadth,$intChocDistributionCount) {
		$intCount = $intChocDistributionCount;
        if($intLength !=0 && $intBreadth !=0){
            if($intLength > $intBreadth){
                $intLength = $intLength-$intBreadth;
                $intCount++;
            }else if($intLength == $intBreadth){
                $intLength = 0;
                $intBreadth = 0;
                $intCount++;
            }else{
                $intBreadth = $intBreadth-$intLength;
                $intCount++;
            }
        }
        return $intCount;
    }

- Trupti Patil June 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function cadbury($input1,$input2,$input3,$input4) {
        $intChocDistributionCount = 0;
		for($i=$input1;$i<=$input2;$i++){
            for($j=$input3;$j<=$input4;$j++){
               $intStudentsCount = calculateCountOfStudents($i,$j,$intChocDistributionCount);
			   $intChocDistributionCount += $intStudentsCount;
            }
        }
        return $intChocDistributionCount-1;
    }

    function calculateCountOfStudents($intLength,$intBreadth,$intChocDistributionCount) {
		$intCount = $intChocDistributionCount;
        if($intLength !=0 && $intBreadth !=0){
            if($intLength > $intBreadth){
                $intLength = $intLength-$intBreadth;
                $intCount++;
            }else if($intLength == $intBreadth){
                $intLength = 0;
                $intBreadth = 0;
                $intCount++;
            }else{
                $intBreadth = $intBreadth-$intLength;
                $intCount++;
            }
        }
        return $intCount;
    }

- Trupti Patil June 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from itertools import product
m=int(input())
n=int(input())
p=int(input())
q=int(input())
student_Count=0;L=[];B=[]

for a in range(m,n+1):
    L.append(a)
for b in range(p,q+1):
    B.append(b)
carton=list(product(L,B)) 

for z in range(0,len(carton)):
    c1=list(carton[z])
    i=c1[0];j=c1[1]
    while(i!=0 and j!=0):
        if(i>j):
            student_Count+=1
            i=i-j
        elif(i<j):
            student_Count+=1
            j=j-i
        else:
            student_Count+=1
            i=j=0
print(student_Count)

- Adish September 29, 2019 | 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