Facebook Interview Question for Software Engineer / Developers






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

Delete(node** head, int k)
{
     node* curr = *head;
     node* prev = NULL;

     while(curr != NULL)
     {
         if(curr->val == k)
         {
             if(prev != NULL)
             {
                 prev->next = curr->next; 
                 delete curr;
                 curr = prev->next; 
             }
             else
             {
                 curr = curr->next;
                 delete *head;
                 *head = curr;
             }
         }
         else
         {
            prev = curr;
            curr = curr->next;
          }
     }
}

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

This comment has been deleted.

- Administrator October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You did not update head ptr. Also ++currptr shld be currptr= currptr->next

- A November 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int delete (node** head,int k)
{
	if(*head->val == k)
	{
		node* temp=*head;
		*head=*head->next;
		free(temp);
		return 0;
	}
	node **n=head;
	while(*n->next->val!=k && *n->next)
	{
		*n=*n->next;
	}
	if(*n->next)
	{
		node* temp=*n->next;
		*n->next=*n->next->next;
		free(temp);
		return 0;
	}
	return -1;
}

- gulusworld1989 October 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will miss the special case where the entire list is made up of nodes of value k. Instead of an if(), your first case should be while(*head->val == k) :)

- Anonymous November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

and this will fail if there are more than one node with the value of k. if should be within while.

- sophia August 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n)

- temprunner October 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in C#:
Node Delete(Node head, int k) // return node as new head
{
while (head != null && head.Data == k)
head = head.Next; // skip initial nodes that equal to k
while (head != null && head.Next != null)
{
if (head.Next.Data == k)
head.Next = head.Next.Next; // delete next node
else
head = head.Next; // advance pointer
}
return head;
}

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

Node* Delete(Node* head, int k)
{
if (! head) return head;

while (head && head->GetValue() == k) {
Node* next = head->GetNext();
delete head;
head = next;
}

if (! head) return head;

Node* pre = head;
Node* node = head->GetNext();

while(node) {
if (node->GetValue() == k) {
Node* next = node->GetNext();
delete node;
pre->GetNext() = next;
node = next;
} else {
pre = node;
node = node->GetNext();
}
}

return head;

}

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

void DeleteElements(Node **head, int k)
{
   if (*head == NULL) return;

   Node *prevNode = *head;
   Node *nextNode = NULL;
   for (Node *cur = head->next; cur != NULL; cur = nextNode) {
       next = cur->next;
       if (cur->val == k) {
           delete cur;
           prevNode->next = next;
       } else {
           prevNode = cur;
       }
   }

   if ((*head)->val == k)) {
      Node *tmp = *head;
      head = (*head)->next;
      delete tmp;
   }
}

- Anon May 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void remove_node(struct link_node **head, int k)
{
     struct link_node *temp;
     while (*head != NULL) {
           if ((*head)->data == k) {
                temp = *head;
                *head = (*head)->next;
           } else {
                 head = &(*head)->next;
           }
     }
}

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

sorry, I forgot to free temp. typing it again.

void remove_node(struct link_node **head, int k)
{
     struct link_node *temp;
     while (*head != NULL) {
           if ((*head)->data == k) {
                temp = *head;
                *head = (*head)->next;
                  free(temp);
           } else {
                 head = &(*head)->next;
           }
     }
}

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

ListNode* delete(ListNode* head, int k)
{
     ListNode* h = head, prev = NULL;
     while (h != NULL)
     {
                if (h->val == k) {
                     if (prev == NULL) head = h->next;
                     if (prev) prev->next = h->next;
                     ListNode* tmp = h;
                     delete h;
                }
                 h = h->next;
     }
}

- Anonymous August 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its just leaves the head pointer, if its its equal to the value.
This can be taken care by the caller.

- vivekh August 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

273 LIST* delete_all_occurences_recursive(LIST* node, int value) {
  274 
  275     LIST* temp;
  276 
  277 
  278     if(node && node->next == NULL)
  279         return node;
  280    if(!node)
  281       return NULL;
  282 
  283   temp = delete_all_occurences_recursive(node->next, value);
  284   if(temp->value == value){
  285       node->next = temp->next;
  286       free(temp);
  287   }
  288   return node;
  289 
  290

}

- vivekh August 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void deleteAllWithK(int i) {
		SLLNode head = list1.head;
		SLLNode node = list1.head.next;
		SLLNode prev = list1.head;
		while(node != null && node != list1.head){
			if(node.data == i)
			{
				prev.next = node.next;
				System.out.println("not in head  - deleting");
				node = null;
			}
			node = node.next;
			prev = prev.next;
		}
		if(head.data == i){
			SLLNode headNode = head;
			head = headNode.next;
			headNode = null;
		}
	}

- Anonymous March 05, 2017 | 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