Microsoft Interview Question


Country: United States




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

@bhardwaj.ankit
According to the question "Sort them such that all the similar number nodes are cluster together". Don't you think that sorting the series will automatically cluster same numbers together? Do let me know if sorting is the requirement or not.

- Learner September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes i think the same too

- t.deepak.iitg September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

sorting is a way to go..but note that they only asked for clustering the numbers together.I think usung a hashmap would get us that in O(n) instead of nlogn for sorting.

- sushanth September 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What do you exactly mean when you say that the similar nodes are clustered together?

- Jack September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nodes which have same values....

- learn September 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then what do you mean by saying "Sort them such that all the similar number nodes are cluster together", any sort anyways will give you the same valued nodes together..

- dhyanchand.munsi September 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If by that you meant the nodes which have same values, wouldn't these nodes already come together only when the list is sorted?

- Jack September 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there is no problem in using additional data structure, I would use a treemap <<value, linkedlist of nodes>> . For some value ->key, node will be linked to the linked list. Since it is a treemap, values will be sorted. So after checking all values, and stroing them in the treemap, we can simply output the nodelist in order.

- musfiqur September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Mushfiqur : How much would the complexity be? TreeMap's internal sorting strategy will also be considered in complexity calculation, right?

- Learner September 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

buble sort on linked list

- anshulzunke September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

....unless you can find a way with complexity less then O(nlgn).. then merge sort.

- yezhuen September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Define "similar" please.

- Richard September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if we use a hashMap
key-->value in the node
value-->linked -list, which contains the nodes with the key value.

Finally, link all the linked-lists in the hashMap together

- Vincent September 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

template <class T>
        void SLList<T>::JoinSimilar()
	{
		LLNode<int> *pCur = pHead;
		if(!pCur || !pCur->pNext) return;
		const int TABLE_SIZE = 10;
		SLList<SLList<int>*> **AggList = new SLList<SLList<int>*>*[TABLE_SIZE]; // hash-table
		for(int ind_table = 0; ind_table < TABLE_SIZE; ind_table++)
		{
			AggList[ind_table] = new SLList<SLList<int>*>; // each element of the table is
		}												// a pointer to the list of lists
		LLNode<int> *pCurTblListNode = NULL, *pTmp = NULL;
		LLNode<SLList<int>*> *pCurTblList = NULL, *pNewTblList = NULL;
		while(pCur)
		{
			pCurTblList = AggList[pCur->iData%10]->pHead; // fill the table element
			while(pCurTblList)
			{
				pCurTblListNode = (pCurTblList->iData)->pHead; // each element is the list of the lists of numbers,
				if(pCurTblListNode->iData == pCur->iData)      // which end with the same digit, e.g.
				{											   // 5->5->5->NULL
					pTmp = pCur->pNext;						   // 35->35->NULL
					pCur->pNext = pCurTblListNode;             // 275->NULL
					(pCurTblList->iData)->pHead = pCur;		   // NULL
					pCur = pTmp;
					break;
				}
				pCurTblList = pCurTblList->pNext;
			}
			if(!pCurTblList)
			{
				pNewTblList = new LLNode<SLList<int>*>;
				pNewTblList->iData = new SLList<int>;
				pNewTblList->pNext = AggList[pCur->iData%10]->pHead;
				AggList[pCur->iData%10]->pHead = pNewTblList;
				(pNewTblList->iData)->pHead = pCur;

				pTmp = pCur->pNext;
				pCur->pNext = NULL;
				pCur = pTmp;
			}
		};
		LLNode<int> *pFisrt = NULL, *pLast = NULL, *pLastTblList = NULL;
		for(int ind_table = 0; ind_table < TABLE_SIZE; ind_table++) // join all the lists into one single list
		{                                                           // and then set the head of the base list to the
			pCurTblList = AggList[ind_table]->pHead;                // new joint list
			while(pCurTblList)
			{
				if(!pFisrt)
				{
					pFisrt = (pCurTblList->iData)->pHead;
				}
				pLast = (pCurTblList->iData)->pHead;
				if(pLastTblList)
				{
					pLastTblList->pNext = pLast;
				}
				while(pLast->pNext)
				{
					pLast = pLast->pNext;
				};
				if(pCurTblList->pNext)
				{
					pLast->pNext = ((pCurTblList->pNext)->iData)->pHead;
					pLastTblList = NULL;
				}
				else
				{
					pLastTblList = pLast;
				}
				pCurTblList = pCurTblList->pNext;
			}
		}
		pHead = pFisrt;

		for(int ind_table = 0; ind_table < TABLE_SIZE; ind_table++)
		{
			delete AggList[ind_table];
		}

- Aleksey.M September 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

FOR GODS SAKE, JUST DO AN INSERTION SORT

- Evan September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think the phrasing 'similar' is intentional. 99 and 999 are similar, but 99, 101, 345, 999 after sorting don't put them together. This is Microsoft. It can't be as simple as sorting.

- dc360 September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is so helpful. Thanks.

- Anonymous September 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Anonymous above did you by any chance forget to give him +1..?

- Prashant R Kumar September 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL! Batteries not included with your sarcasm detector?

- Anonymous September 01, 2012 | Flag


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