Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Written Test




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

Bubble sort should do

- sun November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I do understand but I am not able to implement bubble sort in the code.Can you pls give a start code as to how to implement it?

- Anonymous November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For bubble sort, Have two pointers, one for the data you are checking with and the other one is running pointer. Swap the data instead of manipulating the pointers. I am not sure whether this is optimal.

- Anonymous November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void SortListList(Node **head)
{
int k = 1; // every k pairs of the nodes are sorted at any time;
bool sortCompleted = false;

for (; !sortCompleted; k <<= 1) // double the k on every iteration.
{
Node **h = head;

while (h && *h)
{
Node *first = *h;
Node *second = *h;
for (int i = 0; i < k && second; ++i)
{
second = second->next;
}

if (h == head && !second)
{
sortCompleted = true;
break;
}

Node *result = NULL;
Node *tail = NULL;

int firstRem = k;
int secondRem = k;

while (firstRem && secondRem && first && second)
{
Node **minNode;
int *rem;
if (first->data < second->data)
{
minNode = &first;
rem = &firstRem;
}
else
{
minNode = &second;
rem = &secondRem;
}

Node *toAdd = *minNode;
*minNode = (*minNode)->next;
--(*rem);

if (tail)
{
tail->next = toAdd;
tail = toAdd;
}
else
{
result = tail = toAdd;
}
}

if (firstRem && first)
{
tail->next = first;
}
else
{
tail->next = second;
}

*h = result;

int count = firstRem + secondRem;
while (count && tail->next)
{
tail = tail->next;
}

h = &(tail->next);
}
}
}

- Merge Sort November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void SortListList(Node **head)
{
	int k = 1;  // every k pairs of the nodes are sorted at any time;
	bool sortCompleted = false;

	for (; !sortCompleted; k <<= 1) // double the k on every iteration.
	{
		Node **h = head;

		while (h && *h)
		{
			Node *first = *h;
			Node *second = *h;
			for (int i = 0; i < k && second; ++i)
			{
				second = second->next;
			}

			if (h == head && !second)
			{
				sortCompleted = true;
				break;
			}

			Node *result = NULL;
			Node *tail = NULL;

			int firstRem = k;
			int secondRem = k;

			while (firstRem && secondRem && first && second)
			{
				Node **minNode;
				int *rem;
				if (first->data < second->data)
				{
					minNode = &first;
					rem = &firstRem;
				}
				else
				{
					minNode = &second;
					rem = &secondRem;
				}

				Node *toAdd = *minNode;
				*minNode = (*minNode)->next;
				--(*rem);

				if (tail)
				{
					tail->next = toAdd;
					tail = toAdd;
				}
				else
				{
					result = tail = toAdd;
				}
			}

			if (firstRem && first)
			{
				tail->next = first;
			}
			else
			{
				tail->next = second;
			}

			*h = result;

			int count = firstRem + secondRem;
			while (count && tail->next)
			{
				tail = tail->next;
			}

			h = &(tail->next);
		}
	}
}

- Anonymous November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

thanks all but the code is in C and I am not able not understand as I am dn't have working knowledge of pointers.

- Anonymous November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude
you should read the pointers first and then try with linkedlist because they require knowledge of pointers.

- getjar.com/todotasklist my android app November 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Insertion sort would be more optimal as inserting in a Link List is more easier than swapping(for Bubble sort) in link list...

- Jitesh_K November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As it's posted the list being singly linked list, insertion sort is not viable

- sun November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why?

- Bruce November 21, 2011 | 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