Arista Networks Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

void delete(Node **l, int target)
{
	if( *l == null )
		return;
		
	Node* head = *l;
	Node* curr = *l;
	Node* prev = null;	

	while( curr != null )
	{
		if( curr->data == target )
		{
			if( curr == head )
			{
				head = head->next;
				delete curr;
				curr = head;
			}
			else
			{
				prev->next = curr->next;
				delete curr;
				curr = prev->next;
			}
		}
		else
		{
			prev = curr;
			curr = curr->next;
		}
	}
	
	*l = head;
}

- thomas March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is better move below code out of loop, so to reduce time cost.

if( curr == head )
{
head = head->next;
delete curr;
curr = head;
}

- Ethan March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is wrong because the head can change multiple times throughout the loop.

EX: if the linked list is 5->5->5->1

and you want to delete 5 it will not compute the correct result

- Rohit September 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bool DeleteNode(Linklist *p,int a)
{
	static Linklist head=NULL;
	if(head==NULL)
	{
		head=*p;
		if(head->data==a)
		{
			if((*p)->next==NULL)
			{
			*p=NULL;
			return 0;
			}
           *p=(*p)->next;
		   free(head);
		   head=NULL;
           return DeleteNode(p,a);
		}
	}
	if((*p)->next==0) return 0;
	if(a!=(*p)->next->data)
	{
		 return DeleteNode(&((*p)->next),a);
	}
	else
	{
     Linklist q=(*p)->next;
	 (*p)->next=q->next;
	 free((q));
	 if((*p)->next!=NULL)
	 {
	 return DeleteNode(&((*p)->next),a);
	 }
	}
	return 1;
}

- Charles December 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

void deleteRep(LNode **l, int target){
if(!(*l)) return;

LNode *p = *l;

while(p){
if(p->data == target){
if(p == *l){
*l = p->next;
free(p);
p = *l;
}
else{
LNode *q = p;
p = p->next;
free(q);
}
}
else{
p = p->next;
}
}
}

- Lily March 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void delete(Node **l, int target){
if(*l == null) return;
Node temp,nxt;
//setting the head node whose value is not equal to target
while(((*l) != null) && ((*l)->data == target) ){
temp = *l;
*l = (*l)->next;
free(temp);
}
if(*l == null) return;
temp = *l;
nxt = temp->next;
//iterate over rest of the nodes
while(nxt != null){
if(nxt->data == target){
temp->next = nxt->next;
nxt->next = null;
free(nxt);
nxt = temp->next;
}
else{
temp = nxt;
nxt = nxt->next;
}
}
}

- rkt March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void
delete_node(node_t **currp, int target)
{
        node_t  *tofree;

        while (*currp) {
                if ((*currp)->data == target) {
                        tofree = *currp;
                        *currp = (*currp)->next;
                        free(tofree);
                } else {
                        currp = &(*currp)->next;
                }
        }
}

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

you are changing the head of the list when you delete an element in the middle of the list.

void del_node (node_t **l, int value)
{
node_t *prev = NULL;
node_t *cur = *l;

while (cur) {
if (cur->data == value) {
node_t *t = cur;
if (prev) {
/* Not list head */
prev->next = cur->next;
} else {
/* List head */
*l = cur->next;
}
cur = cur->next;
free(t);
} else {
prev = cur;
cur = cur->next;
}
}
}

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

void delete (Node **I, int target) {

Node *head=NULL;
Node *tmp=NULL;
Node *tail=NULL;
Node *next=NULL;


tmp = *I;
while (tmp) {
if (tmp->data == target) {
next = tmp->next;
free(tmp);
tmp = next;
next = NULL;
} else {
if (!head) {
head = tmp;
tail = tmp;
} else {
tail->next = tmp;
tail = tmp;
}
tmp = tmp->next;
}
}
if (!tail) {
tail->next = NULL;
}

*I = head;
return;

}

- code_sitar February 18, 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