Microsoft Interview Question for SDETs


Team: Cloud
Country: United States




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

void delet(struct node**p)
{
struct node*temp=*p;
*p=(*p)->next;
free(temp);
}

- satyajit January 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 vote

void RemoveHead (node * head)
{
       if(head == Null) return;
       
       node * newHead = head - > next;
       free(head);
       head = newHead;
}

- dumb fellow January 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

head will not get updated with this code....you will need a double pointer as input OR return the new head ptr.

- smallchallenges January 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveHead (node * head)
{
       node * newHead = head - > next;
       free(head);
       head = newHead;
}

- dumb fellow January 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1) What if head is NULL?
2) Maybe return new value of head?

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

void RemoveHead (node *& head)
{
       if(head == Null) return;
       
       node * newHead = head - > next;
       free(head);
       head = newHead;
}

- Anonymous January 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdlib.h>

using namespace std;

struct list_node{
	int data;
	struct list_node * next;
};

typedef struct list_node node;

void add_node( node ** head, int add_data ){

	if( *head == NULL ){
		*head = (node *)malloc(sizeof(node));
		(*head)->data = add_data;
		(*head)->next = NULL; 
	}
	else{
		node *t = (node *)malloc(sizeof(node));
		t->data = add_data;
		t->next = (*head);
		(*head) = t;
	}
}

void print_list( node ** head ){
	node * p = *head;
	while( p != NULL){
		cout << p->data << endl;
		p = p->next;
	}
}

void del_head( node ** head ){

	if( *head == NULL ){
		cout << "operation invalid" << endl;
	}
	else{
		node * t = (*head)->next;
		free(*head);
		(*head) = t;
	}
}

int main(){
	
	node * head;
	add_node( &head, 4 );
	add_node( &head, 5 );
	del_head( &head );
	print_list( &head );
	return 0;
}

- nharryp January 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wonder if it is forbidden to modify the return type to node *.

node * removeHead(node * head) {
  if(head == null) return null;
  node * next = head -> next;
  free(head);
  return next;
}

- cychoi January 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveHead (node ** head)
{
  mutex_lock(&_head_mutex);
   if(head == null || *head == null)
	return;
    node *h = (*head)->next;
    free(*head);
    (*head) = h; 	
   mutex_unlock(&_head_mutex);
}

- Noobie February 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction:

if(head == null || *head == null) {
       mutex_unlock(&_head_mutex);
	return;
}

- Noobie February 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is an excellent interview question. Anyone fails to answer this question should not be hired. A very fundamental question between pionter and pointer to pointer. Pointer to pointer should be used anywhere potentially can modify where the pointer points to. In this case "free" is to change where the pointer points to, from a valid address to null after free.

Any function in linked list that can call malloc/free should use pointer to pointer. Please refer to this blog about more common APIs.
cpluspluslearning-petert.blogspot.co.uk/2014/08/data-structure-and-algorithm-linked.html?m=1

- peter tang February 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not about double pointer / single pointer usage. thats very normal. Its just tricky question. Instead of freeing head node, free next node after copying next node data to head node.

- sajeesh sidharthan April 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveHead(Node *head)
{
Node *next = head->next;
head->data = next->data; /* OR memcpy */
node->next = next->next;
free(next);
}

- sajeesh sidharthan April 02, 2015 | 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