Microsoft Interview Question for Software Engineer / Developers






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

step 1: sort the people by their weights (in ascending order) --- O(nlogn)
step 2: compute the longest increasing subsequence in height --- O(nlogn)

- Anon May 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 0 vote

Looks like an LCS to me:
1. assign an identifier (say a letter, A, B, C, D) to each person
2. create two lists: one sort weight in ascending order, the other sort height in ascending order; in both lists, each person is represented by the letter assigned in step 1
3. find the LCS of the two lists

- GaryZ December 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sounds Brilliant

- Anonymous December 10, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LCS would need O(n^2) in the worst case

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

I think this is solvable by using greedy strategy.

Keep two sorted lists according to height and weight, respectively. Then, for each element, el, in the sorted list, say height, check its weight in the other sorted list. If el weights more than one persons who are supposed to stand below of him, then remove el. If there is just one person, pl, below el weights less than el, then check how many persons pl overweights below him.

This algorithm is supposed to be nlogn.

- Bill May 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Look like O(n*n)

- m July 22, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the ht and wt such that
ht[i]/wt[i] > ht[i+1]/wt[i+1]
i think this will give the ans..

- sn May 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SN, your algo doesn't work. You can't sort based on Height/Weight proprtion/ratio. For e.g. the ratio of 65/100 is > than 68/110 but 65/100 person will be above 68/110 Person.

- Indian May 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this problem can be solved using graphs...

let say the people be (h1, w1), (h2,w2), (h3,w3), (h4,w4),.......
When (h1, w1) comes becomes node for the graph then (h2,w2) comes and satisfies to be on the top or bottom of former then added to the same node of the graph else it will start the new graph... so on the (hN,wN) comes will be added between the links according to there hieght and wieght, else they will start the new graph, or may be added to the same graph. Then the longest tower will be decided by finding the two farthest points in the graph and then comparing among various graph which line is longest

- anks May 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) get the ratio of weight and height
2) order the ratio
3) order all the pairs by weight
4) check if all the height become ordered by doing 3)
5) if 4) is true return
6) if 4) is false discard the pair whose ratio is furthest from a median ratio value of 2) <no matter the biggest or the mallest>
7) start from the 3) again

- ms_emp June 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1) What do you mean get the ratio of weight and height? Ratio of an individual person or average ratio of all people?

2) What do you mean by order the ratio? Order all people by their ratio?

Thanks

- joe_shmo June 29, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think greedy algorithm would work. Here's my solution:
sort the people by their weight, then we can get a list of height with their correspoding weight increasing. Assume the list of height is h1,h2,h3...,hn. for i from 1 to n-1, compare h_i with h_(i+1), if h_i > h_(i+1), remove h_i from the list. The final size of list is the largest possible number.

- lensbo July 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a classic Knapsack problem. Can be solved using dynamic programming with branch and bound. I believe the algorithm is given in Cormen-Rivest Algorithm book.

- anonymous July 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we use radix sort here ? sorting first by weight and then by height ?

- deepak July 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not the trivial version as we may have to remove people here

- NK June 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a DAG from this, for each indegree equal to 0, start the BFS. Compare the result and get the deepth path.

- this way August 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that we have enough personal for the tower (make some checks count > 1).
We have this structure:
struct Aero
{
int height;
int weight;
};
Aero personal[N];

Sort by height.
(The following pic represent the sorting regardless the weights. The wide of the rectangles represents the weight of the player:

|.|
|.|

|..|
|..|
|..|

|...| <- Current
|...|
|...|
|...|

|..| <- Taller but with less weight
|..| <- Is should be not here
|..| <- (*)
|..|
|..|

|....|
|....|
|....|
|....|
|....|
|....|

We can use linked list to represent all the player. Using merge sort we going to sort the list according to their heights. (Merge sort gives O(nlogn) in the worst case). After the sorting we run again thru the list and if the element->next has less weight then current delete next element. In our case delete (*). The complexity is something like O(n + n.log(n)).

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

the tricky inputs may be like this:
70 100
60 110
50 120
in this case only one person can stand ;o)

//algorithm
(1) sort by weight and find longest increasing subseuence in height data O(n^2, n^2)..i mean O(time, space)
(2) sort by height and find longest increasing subseuence in weight data O(n^2, n^2)
(3) take that longest amongst the above 2

- mail2vcp@gmail.com October 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this seems correct...

- T November 25, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Anon

pls let me know how longest increasing subsequence can be done in O(n log n)

- mail2vcp@gmail.com October 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a problem to find the longest path of a DAG.
For any two person A(H1, W1) and B(H2, W2), if (H1<H2 && W1<W2), we have A->B, if (H1>H2 && W1>W2), we have B->A.
If we add (min height, min weight), and (max height, max weight), we will have a start point and end point. After we create this DAG, we find the longest path from start point to end point.

- Yixin November 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not to use BST
Add 1st node as root node...
Compare next node with root-node value..
if(its ht < htroot && wt < wtroot)
insert as left-child
else if (its ht > htroot && wt > wtroot)
insert as right-child

Print tree inorder traversal for answer

Kindly Review :)

- Shwetank Gupta November 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The root might not in the longest sequence.

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

First sort according to weights. Then find the noncontinuous longest increasing sequence for heights.

- barry December 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this: build a min heap using a less operator defined like this:

//sort by weight first, then by height
bool Person::less_1(const Person& other) const
{
    if (weight < other.weight) return true;
    if (weight > other.weight) return false;
    return height < other.height;
}

max_1 is where the shape of the heap is a perfect triangle.

Then we build another min heap, this time with the operator less_2 sorting by height first, then by weight, and again max_2 is the max number of people for which the heap has a perfect triangle shape (i.e. we discard the last level if it does not have precisely N * 2 leaves).

The final solution is max(max_1, max_2).

I assume that each level of the human tower (going from base to top) has N / 2 people than the previous one, but a circus tower could also be formed with N, N - 1, N - 2... not clear from the problem spec.

Thoughts?

- cristi.vlasceanu July 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this: build a min heap using a less operator defined like this:

//sort by weight first, then by height
bool Person::less_1(const Person& other) const
{
    if (weight < other.weight) return true;
    if (weight > other.weight) return false;
    return height < other.height;
}

max_1 is where the shape of the heap is a perfect triangle.

Then we build another min heap, this time with the operator less_2 sorting by height first, then by weight, and again max_2 is the max number of people for which the heap has a perfect triangle shape (i.e. we discard the last level if it does not have precisely N * 2 leaves).

The final solution is max(max_1, max_2).

I assume that each level of the human tower (going from base to top) has N / 2 people than the previous one, but a circus tower could also be formed with N, N - 1, N - 2... not clear from the problem spec.

Thoughts?

- cristi.vlasceanu July 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this: build a min heap using a less operator defined like this:

//sort by weight first, then by height
bool Person::less_1(const Person& other) const
{
    if (weight < other.weight) return true;
    if (weight > other.weight) return false;
    return height < other.height;
}

max_1 is where the shape of the heap is a perfect triangle.

Then we build another min heap, this time with the operator less_2 sorting by height first, then by weight, and again max_2 is the max number of people for which the heap has a perfect triangle shape (i.e. we discard the last level if it does not have precisely N * 2 leaves).

The final solution is max(max_1, max_2).

I assume that each level of the human tower (going from base to top) has N / 2 people than the previous one, but a circus tower could also be formed with N, N - 1, N - 2... not clear from the problem spec.

- cristi.vlasceanu July 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The challenge for this problem is the longest sequence might NOT start from min(heigth) or min(weight) elements.

DP solution. O(n^2).

public class LP : List<Person>;

	List<LP> DP(PA, N)
	{
		List<LP> llp = null;
		if(1 == N) 
		{
			llp = new List<LP>(new LP(PA[0]));
		}
		else 
		{
			llp = DP(PA, N-1);
			foreach(LP lp in llp)
			{
				if(lp.End.height < PA[N].height)
					lp.Append(PA[N]);
			}
		}
		
		return llp;
	}

	void Main()
	{
		//....
		
		// Sort person by weight into array PA[N]
		
		// search all possible sequence
		List<LP> llp = DP(PA, N);
		
		// go through the llp and find the max length of list. Might multiple.
		
		//....
	}

- Anonymous August 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes
sorry, missed one line code in DP(): before the end of "else" clause, add: {{{ else { ... llp.Add(new LP(PA[N])); } } - Anonymous August 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. sort by wt
2. sort by ht
3. find the common longest sub sequence

- Roger November 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Exactly... Thats the solution

- Anonymous December 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong solution.

Height: ABCDEFG
Weight: AFBECDG

LCS: CD
Expected output: ABCD

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

@Anonymous: In your case the LCS is ABCD, not CD. Roger's solution is correct

- Simon December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

According to me, create a graph by analyzing each vertex. There will be an edge if two vertex can satisfy given condition (in case of A(56,90) B(60,95), edge from A->B)
Once formed the graph, get the longest path in the graph (by inverse Dijkstra's algorithm, by having each node as a source at a time)
complexity: forming a graph = O(n^2)
Dijkstra's algorithm = O(n * log n)

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

I've tried applying the tallest tower finding algorithm (9.10), but it doesn't work. Any ideas?

public static ArrayList<HtWt> getIncreasingSequence(ArrayList<HtWt> people) {
		if (people == null) {
			return null;
		} else if (people.size() == 0) {
			return people;
		} else {
			return getIncreasingSequence(people, null);
		}
	}

	public static ArrayList<HtWt> getIncreasingSequence(ArrayList<HtWt> people,
			HtWt bottom) {
		ArrayList<HtWt> max_list = new ArrayList<HtWt>();
		for (int i = 0; i < people.size(); i++) {
			if (canBeAbove(people.get(i), bottom)) {
				ArrayList<HtWt> new_list = getIncreasingSequence(people,
						people.get(i));
				if (max_list == null || new_list.size() > max_list.size()) {
					max_list = new_list;
				}
				if (bottom  != null)
					max_list.add(people.get(i));
			}			
		}
		return max_list;
	}

	public static boolean canBeAbove(HtWt compare, HtWt bottom) {
		if (bottom == null) {
			return true;
		}
		return compare.Ht < bottom.Ht && compare.Wt < bottom.Wt;
	}

- fluffychaos October 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 0 vote

Why not to use BST
Add 1st node as root node...
Compare next node with root-node value..
if(its ht < htroot && wt < wtroot)
insert as left-child
else if (its ht > htroot && wt > wtroot)

Kindly review :)
insert as right-child

Print tree inorder traversal for answer

- Shwetank Gupta November 18, 2008 | 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