Cisco Systems Interview Question for Software Engineer / Developers


Country: United States




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

void delete(Node*& n, int elem) {
  if (!n) return;
  Node* curr = n;
  Node* prev = NULL;
  while (curr != NULL) {
    Node* curr_next = curr->next;
    if (curr->val == elem) {
      delete curr;      
      if (prev) prev->next = curr_next;
      else n = curr_next; 
    }
    else prev = curr;
    curr = curr_next;
  }
}

- Martin August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

this is simple algo ,traverse the list ,delete the node whenever value matches.
start node is special case which is handled saperatly .

void delete(int x,node *start)
{
node *current=start;
while(current)
{
if(current->value==x)  //if we have to delete start node
{
if(current==start)
{
node *temp=start;
current=current->next;
start=current;
delete(node);
}
else
{
node *temp=current->next;
current->value=current->next->value;
current->next=current->next->next;
current=current->next;
delete(temp);
}
}
}

- zeroByzero August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code above has a bug for deleting the last element. Also, it's a dead-loop because there's no current=current.next statement at the end of the while loop.

- Yev August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey thanx for pointing out about bug to delete last element, but its not dead loop as in both if and else current=current is there, have a look again

- zeroByzero August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is no else to outer if how do we move ahead?

- jsumedh August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the first if condition is not satisfied i.e. current->value and x do not equal?Won't we require a current=current->next in the else part of 1st if?

- Suman August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Recursion wud be the best to solve this with the least lines of code.
// Main Call
public void deleteMatching(int value){
if(head == null)
return;
else{
Node newNode = head.nextNode;
deleteMatching(newNode,value);
}
}
//Recurse Call
public void deleteMatching(Node toDelete,int value){
if(toDelete.nextNode.value == value){
Node newNext = toDelete.nextNode.nextNode;
toDelete.nextNode = newNext;
}
else{
toDelete = toDelete.nextNode;
deleteMatching(toDelete,value);
}
}

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

there is another big bug for this code, start=current will not change pointer after the function is over, we should use a 'return to the first node', or use reference of the pointer.

- wenlei.apply March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How would you guarantee current->next=current->next->next; current->next is not NULL, it is not checked and accessing further current->next->next would crash

- Sekhar Pedamallu February 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static LinkedListNode delete_given_number(LinkedListNode head,int givenNumber){
if(head==null){
return null;
}
while(head.data==givenNumber){
head=head.next;
if(head==null){
return null;
}
}
LinkedListNode pervious=head;
LinkedListNode current=head;
while(current!=null){
if(current.data==givenNumber){
pervious.next=current.next;
}
else{
pervious=current;
}
current=current.next;
}

return head;
}

- Chengyun Zuo August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Full working code : awesomecoder.blogspot.com/2012/08/delete-all-matching-elements-in-linked.html

Node* deleteNode(Node* head,int data){
 Node* temp;
 Node* n;
 temp = head;
 while(temp != NULL){
  if(temp->data == data){
   head = temp->next;
   temp->next = NULL;
   free(temp);
   temp = head;
   continue;
  }
  n = temp->next;
  if(n != NULL && n->data == data){
   temp->next = n->next;
   n->next = NULL;
   free(n);
   continue;
  }
  if(n == NULL){
   break;
  } else {
   temp = temp->next;
  }
 }
 return head;
}

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

Here's a solution which works for Nodes<ValueType>

static <ValueType> Node<?> remove(Node<?> head, final ValueType targetValue) {
        Node<?> start = head;

        //System.out.println("Target value: " + targetValue);

        // handle base cases
        if (head == null) {
            return null;
        } else if ((head != null) && (head.next == null) &&
                targetValue.equals(head.value)) {
            return null;
        } else if ((head != null) && (head.next != null) &&
                (head.next.next == null) && targetValue.equals(head.value)) {
            return start = head.next;
        }

        //System.out.println("Start b4 loop: " + start);
        //System.out.println("Head b4 loop: " + head);

        while (head != null) {
            //printList(start);

            if (targetValue.equals(head.value)) {
                head = head.next;
                start = head;
            } else if (head.next!=null && targetValue.equals(head.next.value)) {
                head.next = head.next.next;
            } else {
                head = head.next;
            }

            //System.out.println("Start post loop: " + start);
            //System.out.println("Head post loop: " + head);
        }

        return start;
    }

Test output:
Original list: 3 1 2 3 3 5 3 
Modified list: 1 2 5

- Yev August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct LinkedList* deleteDuplicate(struct LinkedList *head, int data)
{
	struct LinkedList *flag , *temp=head;
	int i;
       //delete duplicate data  if start from first node
	while( (head) && (head)->data == data)  
	{
		temp = head;
		head = temp->next;
		if(temp)
		{
			temp->next = NULL;
			free(temp);
		}	
	}

// delete node if repeated data after the start node
	while(temp)
	{
		flag = temp;
		temp = temp->next;	
		if(temp  && temp->data == data)
		{
			flag->next = temp->next;
			temp->next = NULL;
			free(temp);
			temp = flag;
		}
	}
return head;
}

- Aalok August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

prev=head;
cur=head->link;
while(cur!=NULL)
{
if(value==cur->data)
{
temp=cur;
cur=cur->link;
prev->link=cur;
free(temp);
}
else
{
prev=prev->link;
cur=cur->link;
}
}

- Gaurav August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java version:

public void delete(int value){
		Node current = this.head;
		Node prev = null;
		if(current == null)
			return;
		while(current!=null){
			if(current.getData() == value){
				if(prev == null){
					this.head = current.getNext();
					current.setNext(null);
					current = this.head;
				}else{
					prev.setNext(current.getNext());
					current.setNext(null);
					current = prev.getNext();
				}
			}
			else{
				prev = current;
				current = current.getNext();
			}
		}
	}

- Anonymous September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void deleteData(int data)
	{
       if(root == null ) return;
       
       while(root != null && root.getData() == data)
    	   root = root.next;
       
       Node prev = root;
       Node next = root;
       
       while(next != null)
       {
    	   
    	   if(next.getData() == data)
    	   {
    		   prev.next = next.next;
    	   }
    	   else 
    	   {   
    		   prev = next;		
    	   }
    	   
    	   next = next.next;
       }
		
		
	}

- nmc October 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void deletecopies(struct listnode *head, int data)
{
	struct listnode *current =head; temp =head;

	while ( current)
	{
		if ( current->data == data)
		{
			if (current == head )
			{
				head = head->next;
				free(current);
				current = head;
			}
			else
			{				
				temp->next = current->next;
				free(current);
				current = temp->next;
			}
			continue;

		}
		temp = current;
		current = current->next;
	}
}

- Sekhar Pedamallu February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cisco gives hike once in 2-3 years.

New-Joinees only get hike after 2-3 years, so take 100% hike at the time of joining .. . otherwise don't cry after joining. :)

- Simple April 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One solution is to gave 3 Nodes- slow, current and fast. Iterate over the list :
if(current.data == input value) {
slow.next=fast;
}

- RishabG June 25, 2016 | 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