## 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;
}
}``````

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);
}
}
}``````

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

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.

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

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

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

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

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

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?

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

Recursion wud be the best to solve this with the least lines of code.
// Main Call
public void deleteMatching(int value){
return;
else{
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);
}
}

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

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.

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

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

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

return null;
}
return null;
}
}
while(current!=null){
if(current.data==givenNumber){
pervious.next=current.next;
}
else{
pervious=current;
}
current=current.next;
}

}

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

``````Node* deleteNode(Node* head,int data){
Node* temp;
Node* n;
while(temp != NULL){
if(temp->data == data){
temp->next = NULL;
free(temp);
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;
}
}
}``````

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) {

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

// handle base cases
return null;
return null;
}

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

//printList(start);

} else {
}

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

return start;
}

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

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

``````struct LinkedList* deleteDuplicate(struct LinkedList *head, int data)
{
int i;
//delete duplicate data  if start from first node
{
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;
}
}
}``````

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

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

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

Java version:

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

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;
}

}``````

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

``````void deletecopies(struct listnode *head, int data)
{

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

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

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. :)

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;
}

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.

### 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.