Interview Question for Software Engineer / Developers






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

Can anyone check please this code? Thanks!

int lowestCost(int N,int* a, int left, int right)
// a is the array of the number of characters in each segment, in the example it is [2,6,2,10]
// left and right are the indices of array a, a[left]+a[left+1]+...+a[right-1]+a[right] should = N
{
int min=999999;
for(int i=left;i<=right-1;i++)
{
int costsFromLeft=0;
int costsFromRight=0;

cout<<"i="<<i<<endl;
if(i>left)
{
int sumLeft=0;
for(int j=left;j<=i;j++)
sumLeft+=a[j];

cout<<"cutting "<<sumLeft<<" from:"<<left<<" to:"<<i<<endl;
costsFromLeft=lowestCost(sumLeft,a,left,i);
}
if(right>i+1)
{
int sumRight=0;
for(int j=i+1;j<=right;j++)
sumRight+=a[j];

cout<<"cutting "<<sumRight<<" from:"<<i+1<<" to:"<<right<<endl;
costsFromRight=lowestCost(sumRight,a,i+1, right);
}

if(costsFromLeft+costsFromRight<min)
min=costsFromLeft+costsFromRight;

}
return min+N;
}

To start, we call
int l=lowestCost(N,a,0,length_of_a-1);

(It should be easy to convert L to a)

- Anonymous October 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone check please this code? Thanks!

int lowestCost(int N,int* a, int left, int right)
// a is the array of the number of characters in each segment, in the example it is [2,6,2,10]
// left and right are the indices of array a, a[left]+a[left+1]+...+a[right-1]+a[right] should = N
{
int min=999999;
for(int i=left;i<=right-1;i++)
{
int costsFromLeft=0;
int costsFromRight=0;

cout<<"i="<<i<<endl;
if(i>left)
{
int sumLeft=0;
for(int j=left;j<=i;j++)
sumLeft+=a[j];

cout<<"cutting "<<sumLeft<<" from:"<<left<<" to:"<<i<<endl;
costsFromLeft=lowestCost(sumLeft,a,left,i);
}
if(right>i+1)
{
int sumRight=0;
for(int j=i+1;j<=right;j++)
sumRight+=a[j];

cout<<"cutting "<<sumRight<<" from:"<<i+1<<" to:"<<right<<endl;
costsFromRight=lowestCost(sumRight,a,i+1, right);
}

if(costsFromLeft+costsFromRight<min)
min=costsFromLeft+costsFromRight;

}
return min+N;
}

To start, we call
int l=lowestCost(N,a,0,length_of_a-1);

(It should be easy to convert L to a)

- Bao Lei October 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

could you please give me the computational rule for your dynamic programming solution?

- Jit October 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Say N=20, the 4 segments are 2,6,2,10

So we try all the following:
2 vs 18(6+2+10), 8(2+6) vs 12(2+10), 10(2+6+2)+10
see which is the lowest cost.
Then return 20 + the lowest cost

when we deal with 18(6+2+10), we can use the same rule
(try 6vs12 and 8vs10)

- baolei1234 October 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry for the duplicate reply and failure to include white space..
it's my first time to use this forum.

what is the best way to post code, so that the indents can be displayed? Thanks.

- baolei1234 October 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is essentially the same question as http :// www . careercup.com/question?id=3622698
User (rockaustin2k6) on that page offered a decent solution.

- Anonymous October 15, 2010 | 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