Whats wrong with my code! Microsoft didn't continue interview after reviewing my code !! please help


Forum Post 9 Answers Whats wrong with my code! Microsoft didn't continue interview after reviewing my code !! please help

Hello Friends,
I was being interviewed by Microsoft for one of their Live groups. They asked me to send code for the following questions.
I don't find anything wrong with my code ( I intensionally left out NULL pointer checks because they were obvious and I didn't
want to pollute the code for the "review"). Could you please tell me why they have discontinued the interview process ?

1. Write function to find a string in a string i.e. strstr
2. Write code to reverse a linked list
3. How would you most efficiently return the middle node of a singly-linked list? (provide code for this solution)
4. How would you remove the duplicate characters from an array? (provide code for this solution)

#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////
//1. Write function to find a string in a string i.e. strstr
///////////////////////////////////////////////////////////////

//KMP implementation//
/*
     The mystrstr() function locates the first  occurrence  of  the
     string  str2  (excluding  the  terminating  null character) in
     string str1 and returns a pointer to the located string, or  a
     null  pointer if the string is not found. If  str2 points to a
     string with zero length (that is, the string ""), the  func-
     tion returns  str1.

*/
void makeTable(const char* str,int* T,int size);

const char* mystrstr(const char* str1,const char* str2){
	int n = strlen(str1);	
	int m = strlen(str2);
	
	if(m==0)
		return str1;
	if(m > n)
		return NULL;
	
	int* T = (int*)malloc(sizeof(int)*m);
	makeTable(str2,T,m);
	
	int a = 0;
	int b = 0;
	int c = 0; // match count
	
	while((a + c) < n){
		if(str1[a + c] == str2[c]){
			c++;
			if(c==m){
				free(T);
				return (str1 + a); 
			}
		}
		else if(c==0){
			a++;
		}
		else{
			a += c - T[c];
			c = T[c];
		}
	}
	
	free(T);
	return NULL;
}

void makeTable(const char* str,int* T,int size){
	int a = 0;	
	int b = 0;
	int c = 0;
	T[a] = 0;
	for(a=1;a<size;a++){
		if(str[b] == str[a]){
			c++;
			b++;
			T[a] = c;
			continue;
		}
		else if(b > 0){
			a--;
		}
		c = 0;
		b = 0;
	}
}

////////////////////////////////////////////////////
//2.  Write code to reverse a linked list
///////////////////////////////////////////////////
struct node {
		int val;
		node* next;
};


void createSampleList(node** head,int size){
	node* n = NULL;
	node* H = NULL;
	for(int i=0;i<size;i++){
		n = new node();
		n->val = i;
		n->next = H;
		H = n;
	}
	*head = H;
}

void printList(const node* n){
	while(n!=NULL){
		cout << n->val <<" ";
		n = n->next;
	}
	cout << endl;
}


/// Iterative solution ////////////
void reverse(node** head){
	node *Rhead = NULL;
	node* H = *head;
	while(H){
		node* tmp = H->next;
		H->next = Rhead;
		Rhead = H;
		H = tmp;
	}
	*head = Rhead;
}
//////Recursive solution ///////////
node* recreverse(node* prev,node* cur);

//Initialization
void recursive_reverse(node** head){
	if(!*head)
		return;
	if(!(*head)->next)
		return;
		
	node* next = (*head)->next;
	(*head)->next = NULL;
	*head = recreverse(*head,next);
}

//Recursive Function
node* recreverse(node* prev,node* cur){
	node* next = (cur)->next;
	(cur)->next = prev;
	if(next == NULL)
		return cur;
	return recreverse(cur,next);
}
	


//3. How would you most efficiently return the middle node of a singly-linked list? (provide code for this solution)
/*
 * Explanation: Keep two pointers, fast and slow. the fast pointer jumps 2 nodes at a time while slow pointer
 *              jumps only one node. When the fast node point to Nth node, slow pointer will point to floor(N/2)th node 
 * 
 * Complexity O(N) where N is size of the list
 * */

const node* getMiddleNode(const node* head){
	if(!head)
		return NULL;
	if(!head->next)
		return head;
	
	const node* slow = head;
	const node* fast = head;
	
	while(fast){
		fast = fast->next;
		if(fast){
			fast = fast->next;
			if(fast)
				slow = slow->next;
		}
	}
	
	return slow;
}

//4. How would you remove the duplicate characters from an array? (provide code for this solution)

char* remdupchar(char* str){
	if(strlen(str)<2)
		return str;
	char* a = str;
	char* b = str+1;
	
	while(*a != '\0'){
		if(*a != *b){
			a++;
			*a = *b;
		}
		b++;	
	}
	
	return str;
}




///////////////////////////////////////////////////////////////////////
//// ==============  Test Functions ============================ ////
////////////////////////////////////////////////////////////////////////



void testList(){
	cout <<"======List Test======"<<endl;
	
	node* head;
	
	createSampleList(&head,10);
	printList(head);
	reverse(&head);
	printList(head);
	recursive_reverse(&head);
	printList(head);
	
	const node* middlenode = getMiddleNode(head);
	if(middlenode)
		cout <<"Middle node is :"<<middlenode->val<<endl;
}

void testStrstr(){
	cout <<"======strstr() Test======"<<endl;	
	const char* pattern = "abcde";
	const char* text = "xxababcdabccccccxabcdeyyyyyyyy";
	
	char* begin = (char*)mystrstr(text,pattern);
	if(begin){
		cout << begin << endl;
	}
	else
		cout <<"String not found"<<endl;
}

void testRemoveDups(){
	cout <<"======remdupchar Test======"<<endl;
	char str[100];
	strcpy(str,"aaaaaaaabcccccddddddeee");
	remdupchar(str);
	cout << str << endl;
}


int main(){
	testList();
	testStrstr();
	testRemoveDups();
}

- Carbon April 28, 2008 | Flag |


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

Hi Kalpesh,

just for the sake of info. I did the prog but it is in java.Taken a HashTable,which will keep the track of character with the funda of collision hashing.Here it goes.

public class DuplicateArr {

public static void main(String[] args) {

String test="nickname";
String orig="";
test=test.toUpperCase();
int temp;
char[] test1=test.toCharArray();
int[] hash= new int[26];
for(int i=0;i<test.length();i++)
{
temp=test1[i]-65;
if (hash[temp]==0)
{
orig+=test1[i];
hash[temp]=1;
}

}
System.out.println("The String without Duplicate: "+orig);

}

}

- Samrat Som May 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, Ur code for remdupchar doesn't work fine... (i just check that one only)...
It will work the type of input u tested, but it will not work of input where duplicates are not adjacent.. like "nickname", "remremremremrem"

- anks May 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I only looked at your first solution, and let me tell you: you _NEED_ to use better variable names, and you need to use more comments. How many of those are 1 letter variables, and if I didn't know what the knuth-morris-pratt algorithm was, how would I know you were doing? No comments makes work on MY end in order to decipher what you are doing - if I am a hiring manger, I want to be able to understand exactly what you are doing without having to use a Rosetta Stone to interpret your code. During on-site interviews less comments are OK cause we are talking though it and I see your thinking as you are writing. When you turn this in and I have to read it so you need to explain your thoughts.

- Random Dude June 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is what I came up with strstr(). Hope this helps to others:
char* strstr1(char* str, char* substr)
{
char* tmpstr = str;
char* tmpsubstr = substr;
char* resultstr=NULL;
bool inmatch = false;
while((*tmpstr!= '\0')) {
if(*tmpstr == *tmpsubstr) {
resultstr = tmpstr;
inmatch = true;
do {
if(*tmpstr != *tmpsubstr) {
inmatch = false;
break;
}
tmpstr++;
tmpsubstr++;
} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
if((*tmpsubstr == '\0') && (inmatch == true)) {
return resultstr;
} else {
tmpsubstr = substr;
resultstr = NULL;
continue;
}
}
tmpstr++;
}
return resultstr;
}

- srik545 December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is what I came up with strstr(). Hope this helps to others:
char* strstr1(char* str, char* substr)
{
char* tmpstr = str;
char* tmpsubstr = substr;
char* resultstr=NULL;
bool inmatch = false;
while((*tmpstr!= '\0')) {
if(*tmpstr == *tmpsubstr) {
resultstr = tmpstr;
inmatch = true;
do {
if(*tmpstr != *tmpsubstr) {
inmatch = false;
break;
}
tmpstr++;
tmpsubstr++;
} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
if((*tmpsubstr == '\0') && (inmatch == true)) {
return resultstr;
} else {
tmpsubstr = substr;
resultstr = NULL;
continue;
}
}
tmpstr++;
}
return resultstr;
}

- srik545 December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-------------------------------------------------
char* strstr1(char* str, char* substr)
{
    char* tmpstr = str;
    char* tmpsubstr = substr;
    char* resultstr=NULL;
    bool inmatch = false;
    while((*tmpstr!= '\0')) {
        if(*tmpstr == *tmpsubstr) {
            resultstr = tmpstr;
            inmatch = true;
            do {                
				if(*tmpstr != *tmpsubstr) {
					inmatch = false;
					break;
                }
                tmpstr++;
                tmpsubstr++;
			} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
			if((*tmpsubstr == '\0') && (inmatch == true)) {
				return resultstr;
			} else {
				tmpsubstr = substr;
				resultstr = NULL;
				continue;
			}
		}
		tmpstr++;
    }
    return resultstr;
}
-------------------------------------------------

- srik545 December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-------------------------------------------------
char* strstr1(char* str, char* substr)
{
    char* tmpstr = str;
    char* tmpsubstr = substr;
    char* resultstr=NULL;
    bool inmatch = false;
    while((*tmpstr!= '\0')) {
        if(*tmpstr == *tmpsubstr) {
            resultstr = tmpstr;
            inmatch = true;
            do {                
				if(*tmpstr != *tmpsubstr) {
					inmatch = false;
					break;
                }
                tmpstr++;
                tmpsubstr++;
			} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
			if((*tmpsubstr == '\0') && (inmatch == true)) {
				return resultstr;
			} else {
				tmpsubstr = substr;
				resultstr = NULL;
				continue;
			}
		}
		tmpstr++;
    }
    return resultstr;
}
-------------------------------------------------

- srik545 December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-------------------------------------------------
char* strstr1(char* str, char* substr)
{
    char* tmpstr = str;
    char* tmpsubstr = substr;
    char* resultstr=NULL;
    bool inmatch = false;
    while((*tmpstr!= '\0')) {
        if(*tmpstr == *tmpsubstr) {
            resultstr = tmpstr;
            inmatch = true;
            do {                
				if(*tmpstr != *tmpsubstr) {
					inmatch = false;
					break;
                }
                tmpstr++;
                tmpsubstr++;
			} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
			if((*tmpsubstr == '\0') && (inmatch == true)) {
				return resultstr;
			} else {
				tmpsubstr = substr;
				resultstr = NULL;
				continue;
			}
		}
		tmpstr++;
    }
    return resultstr;
}
-------------------------------------------------

- srik545 December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-------------------------------------------------
char* strstr1(char* str, char* substr)
{
    char* tmpstr = str;
    char* tmpsubstr = substr;
    char* resultstr=NULL;
    bool inmatch = false;
    while((*tmpstr!= '\0')) {
        if(*tmpstr == *tmpsubstr) {
            resultstr = tmpstr;
            inmatch = true;
            do {                
				if(*tmpstr != *tmpsubstr) {
					inmatch = false;
					break;
                }
                tmpstr++;
                tmpsubstr++;
			} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
			if((*tmpsubstr == '\0') && (inmatch == true)) {
				return resultstr;
			} else {
				tmpsubstr = substr;
				resultstr = NULL;
				continue;
			}
		}
		tmpstr++;
    }
    return resultstr;
}
-------------------------------------------------

- srik545 December 10, 2011 | 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