IBM Interview Question for Software Engineer / Developers






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

Cost=1?

I do get when U say 1 there is only one pair misplaced but how can we be so sure.
Don't we need to scan the array to check whether that the elements are in the right positions.

- VVG August 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

cost = number of operations

- Anonymous August 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

4,3,5,6 can be sorted by performing one decrement on first element. Hence cost=1

- Anonymous August 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No you don't need to scan the array; since you are not allowed to move(or swap) the elements, only thing you need to do is to compare the element with the next element, if its small continue with the next, otherwise decrement it or remove it from the array(whichever costs less)

- anonymous March 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

compare elements in pairs, and decide what to do to sort them.

In 4,3,5,6 --> compare (4,3) --- cost 1 to make 4->3

In 10,3,11,12 --> compare (10,3) --- cost 3 to remove 3

- MB August 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

consider an array 10, 4, 7. What would you do after you run compare(10, 4), which removes 4? Instead of running comparison between 10 and 4, you should keep advancing until you see a value larger than 10.

- tristan.uva August 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what do you mean by looking >10? in your case there is none. what happens then?

- MB August 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Typical dynamic programming thing

- Anonymous August 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Any more details please?

- Anonymous August 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have the sequence say 3 10 7 9 1
Find the longest non-decreasing sequence, which is 3 7 9

If a number is not included in the longest non-decreasing sequence:
check if it can be included by one of the following methods:
1. 10 can be deleted. cost = 10
2. 10 can be decremented to 3 or 7 to be included in the sequence. of cost (10-3) or (10-7). Minimum cost = 3
So, we select option 2.

for 1, it cannot be decremented. so it can be deleted with minimum cost.

- V August 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So the final sequence is : 3 7 7 9 ,with a minimum cost of 4 (3+1)

- V August 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not correct. Consider 1, 1, 1, 1000, 1
Your algorithm will pay 999 for decreasing 1000, when in fact the right answer is 4(delete the 1s)

- Anonymous August 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The algorithm will not decrease 1000 by 999. Since it is going for the largest "non decreasing" subsequence rather than increasing sequence, all that will happen is that the 1 at the end will be deleted. The right answer is infact 1. 1,1,1,1000 is sorted.

- Anonymous August 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What will it do for
10 2 3

I guess it will change 10->1 when the correct answer I guess is to remove 2 and 3 ?

- Aditya September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*


Add an Interview Question


IBM Interview Question for Software Engineer / Developers
Anonymous on August 26, 2010

You are given an array of positive integers. Convert it to a sorted array with minimum cost. Only valid operation are
1) Decrement -> cost = 1
2) Delete an element completely from the array -> cost = value of element

For example:
4,3,5,6, -> cost 1
10,3,11,12 -> cost 3
*/
#include<vector>
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct nn{
	int cost;
	int minimum;
	int maximum;
}costnode;
#define print(arr) for(int i = 0;i<arr.size();i++) cout<<arr[i]<<" ";cout<<endl;
int ComputeMinimumCostSortedArray(vector<int> &arr)
{
	
	int sz = arr.size();
	print(arr);
	vector<costnode> Cost;
	for(int i=0;i<sz;i++)
	{
		costnode c;
		c.cost = 0;
		c.minimum = arr[i];
		c.maximum = arr[i];
		Cost.push_back(c);
	}
	for(int i=1;i<sz;i++)
	{
		vector<costnode> temp;
		for(int j=0;j<sz-i;j++)
		{
			int mincostleft = Cost[j].cost + ((arr[j+i]>Cost[j].maximum)?0:arr[j+i]);
			int mincostright = Cost[j+1].cost + ((arr[j]<Cost[j+1].minimum)?0:(arr[j]-Cost[j+1].minimum));
//			printf("j: %d %d %d LC: %d RC: %d LCMin:%d LCMAx: %d RCMin: %d RCMax: %d ResLeft:%d ResRight:%d %d\n", j, mincostleft, mincostright, Cost[j].cost, Cost[j+1].cost, Cost[j].minimum, Cost[j].maximum, Cost[j+1].minimum, Cost[j+1].maximum, ((arr[j+i]>Cost[j].maximum)?0:arr[j+i]), (arr[j]<Cost[j+1].minimum)?0:(arr[j]-Cost[j+1].minimum), Cost[j+1].cost + ((arr[j]<Cost[j+1].minimum)?0:(arr[j]-Cost[j+1].minimum)));
			costnode c;
			if(mincostleft<mincostright)
			{
				c.cost = mincostleft;
				c.minimum = Cost[j].minimum;
				c.maximum = (arr[j+i]>arr[j+i-1])?arr[j+i]:Cost[j].maximum;
			}
			else{
				c.cost = mincostright;
				c.minimum = (arr[j]<Cost[j+1].minimum)?arr[j]:Cost[j+1].minimum;
				c.maximum = Cost[j+1].maximum;
			}
			temp.push_back(c);
		}
		Cost = temp;
	}
	printf("%d\n", Cost[0].cost);
}
int main()
{
	int n;
	while(1)
	{
		vector<int> arr;
		while(scanf("%d", &n)!=EOF && n!=-1)
		{
			arr.push_back(n);
		}		
		ComputeMinimumCostSortedArray(arr);
	}
}

- Anonymous September 24, 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