Citrix System Inc Interview Question for Software Engineer / Developers






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

/*iterative method:*/
void swap_twonodes(struct node** headref)
{
struct node* current=*headref;
struct node* save=NULL;
if(current==NULL) return; /*empty list*/
if(current->next!=NULL) /*set head pointer to correct position*/
*headref=current->next;
while( (current!=NULL) && (current->next!=NULL) ) /*iterate down,to swap the list by two nodes*/
{
save=current->next->next;
current->next->next=current;
current->next=save;
current=save;
}
}

- Pradeep Rajkumar July 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1->2->3->4 must give 2->1->4->3.
But 1 is not made to point to 4, in ur code..

- Myself July 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below code is generic implementation for reversing a chunks in linked list

void reverseListChunk(int k, node** headRef)
{
	node* first=0,*second=0,*third=0,*temp=0;
	node* prevTemp=0,*revRoot=0,*_head=0,*prevHead=0;
	int count=1,k;

	_head = *headRef;
	temp = *headRef;
	prevHead = *headRef;
	while(temp)
	{
		while((temp)&&(count<k))
		{
			prevTemp=temp;
			temp=temp->next;
			count++;
		}
		first=_head;
		if(first)
		{
			second=first->next;
		}
		while(first != temp)
		{
			if(second)
			{
				third=second->next;
				second->next=first;
			}
			else
			{
				third = 0;
			}
			first=second;
			second=third;
		}
		if(revRoot == 0)
		{
			if(temp)
			{
				revRoot=temp;
			}
			else
			{
				revRoot=prevTemp;
			}
			prevHead->next=0;
		}
		else
		{
			if(temp)
			{
				prevHead->next=temp;
			}
			else
			{
				prevHead->next=prevTemp;
			}
			prevHead=_head;
			prevHead->next=0;
		}
		_head=third;
		temp=third;
		count=1;
	}
	*headRef=revRoot;
}

For this case k=2

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

<pre lang="" line="1" title="CodeMonkey96224" class="run-this">MyList* reverseList(MyList *head)
{
if (NULL == head || NULL == head->next)
return head;

MyList *preNode = head, *curNode = head->next,*nextNode;
MyList *ppreNode = NULL;
MyList *outHead = curNode;
while (curNode)
{
nextNode = curNode->next;
curNode->next = preNode;
preNode->next = nextNode;
if (ppreNode)
ppreNode->next = curNode;
ppreNode = preNode;
preNode = nextNode;
if (preNode)
curNode = preNode->next;
else
break;
}

return outHead;
}

</pre><pre title="CodeMonkey96224" input="yes">
</pre>

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

The solution is pretty simple
here it is in pseudo code

check : for empty list or single element list

next : two pointers say p1 , p2
p1 points to head
p2 points to head->next

loop :
while neither pointer is null
simply exchange satellite data of p1 , p2 not the pointers themselves

- Coder July 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and of course in the loop
p1 = p1->next
p2 = p2->next

- Coder July 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node *reverse(node *head)
{
        if(head == NULL || head->next == NULL)
                return head;

        int count = 0;
        node *prev = head;
        node *cur = head->next;
        // Saving the return pointer, which points to the new head
        node *ret = cur;
        node *fwd;

        while(cur!=NULL)
        {
		//If the node count is odd, you need to set the prev's next pointer to some value
                if(count%2 != 0)
                {
                        if(cur->next!=NULL)
                        {
                                prev->next->next = cur->next;
                        }
                        else
                        {
                                prev->next->next = cur;
                        }

                        fwd=fwd->next;
                        prev=cur;
                        cur=fwd;
                }
		//else the usual reverse logic
                else
                {
                        fwd = cur->next;
                        cur->next = prev;
                        prev->next=NULL;
                        prev = cur;
                        cur = fwd;
                }
                count++;
        }

        return ret;
}

- Vrishti August 23, 2013 | 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