Zynga Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

public Node reverseIterative(Node head) {
		Node current = head;
		Node next = current.next;
		current.next = null;
		Node temp;
		while (next != null) {
			temp = next.next;
			next.next = current;
			current = next;
			next = temp;
		}
		return current;
	}

	public Node reverseRecursive(Node head) {
		if (head.next == null)
			return head;
		Node oldNext = head.next;
		Node newHead = reverseRecursive(head.next);
		oldNext.next = head;
		head.next = null;
		return newHead;
	}

- mykola July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Iterative Approach:
let the pointer 'second' points to first node , the pointer 'first' points to second node and the pointer 'third' points to null.

while(second!=null)
{
     second -> next = third;
     first -> next = second;
     third = second;
    second = first;
    first = first -> next;
}

- cobra July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

To reverse a linked list recursively watch this video, it was really helpful

youtube.com/watch?v=CXjQUdAwRSg

to learn about iterative reversal of linked list watch

youtube.com/watch?v=FRab13yyYms

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

A recursive approach:

rev(root)
{
	if(!root || !(root->next))
		newroot=root;
	else
	{
		rev(root->next);
		if(root->next)
		{
			root->next->next=root;
			root->next=NULL;
		}
	}
	return newroot;
}

An iterative one:

iterativerev(root)
{
	Node *p,*q,*r;
	q=root;
	while(q)
	{
		r=q->next;
		q->next=p;
		p=q;
		q=r;
	}
	return p;
}

- Aashish July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void Reverse(ref Node<int> head)
        {

            Node<int> first;
            Node<int> rest;
            if (head == null) return;
            first = head;
            rest = first.NextNode;
            if (rest == null) return;
            Reverse(ref rest);
            first.NextNode.NextNode = first;
            first.NextNode = null;
            head = rest;

}

- ajaypathak July 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/////////// iterative

LinkNode *reverseLink(LinkNode *head)
{
if(head == NULL)
return NULL;
if(head->next == NULL)
return head;
if(head->next->next == NULL)
{
LinkNode *tail = head->next;
head->next->next = head;
head->next = NULL;
return tail;
}

LinkNode *pre = head;
LinkNode *current = head->next;
LinkNode *next = head->next->next;

while(next != NULL)
{
current->next = pre;
pre = current;
current = next;
next = next->next;
}
head->next = NULL;
current->next = pre;
return current;
}

////////////recursive

LinkNode *reverseLink(LinkNode *head)
{
if(head == NULL)
return NULL;
if(head->next == NULL)
return head;

LinkNode *next = reverseLink(head->next);
next->next = head;
head->next = NULL;
return head;
}

- UNFounder January 06, 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