NVIDIA Interview Question for Software Engineer / Developers






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

I used 3 pointers. Not sure if you can do it with two. If anyone has any suggestions please let me know! handles even # of nodes, odd # of nodes, 0, 1, 2 nodes.

void swap_every_two(node **head) {
  node *current = *head;
  node *next = NULL;
  node *prev = NULL;
  if (head == NULL)
    return;
  if (*head == NULL) /* no elements */
    return;
  if ((*head)->next == NULL) /* only one element */
    return;
  *head = current->next; /* update the head pointer */
  next = current->next;
  current->next = next->next;
  next->next = current;
  prev = current;
  current = current->next;
  while (current != NULL && current->next != NULL) {
    next = current->next;
    current->next = next->next;
    next->next = current;
    prev->next = next;
    prev = current;
    current = current->next;
  }

}

- woohoo February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.break link list into two parts.evenPart 2->4->6 and oddPart 1->3->5->7.
2.now built a link list as even->odd->even->odd->even->odd : 2->1->4->3->6->5

- PKT February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive solution:

void swappairs(node **head)
{
        if(head==NULL ||*head==NULL)
                return;
        if((*head)->next==NULL)
                return;
        node *current = *head;
        *head=(*head)->next;
        current->next=(*head)->next;
        (*head)->next=current;
        swappairs(&(current->next));
}

- krshnsh November 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void swapNodes(Node **head) {
	if(!*head)
		return;
	int first = 1;
	Node *newHead;
	while((*head)->next) {
		Node *tmp = *head;
		Data data = (*head)->data;
		displayData(data);
		Node *next = (*head)->next;
		Node *after = NULL;
		if(next->next)
			after = next->next;
		(*head)->data = next->data;
	    (*head) = next;
	    next->data = data;	
		next = tmp;
		next->next = (*head);
		if(first) {
			newHead = next;
			first = 0;
		}
		(*head)->next = after;
		if(after)
			(*head) = after;
	}
	*head = newHead;
}

- moh September 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

lnode *swap_two(lnode *start)
{
lnode *ret;
lnode *next_two;

// Terminate Recursion
if (start == NULL || start->next == NULL)
return (start);

next_two = start->next->next;
ret = start->next;
start->next->next = start;
start->next = swap_two(next_two);

return (ret);
}

Functions return linked list in such order.

- LouisK March 10, 2018 | 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