Microsoft Interview Question for Program Managers






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

Pretty much same logic as prolific coder. But in C++

//Assumptions 2 null terminated strings. Original string will be destroyed
//Program insert the ' ' instead of the delimiters

void func(char* str, char* str1)
{
map<char,int> map1;
int i=0;

//Insert all delimiters in the map
for(i=0;i<strlen(str1);i++)
map1[str1[i]]++;

// If this char is a delimiter, insert space instead of the char
for(i=0;i<strlen(str);i++)
{
if(map1[str[i]]>0)
str[i]=' ';
}
}


int main(void)
{
char str[]= "abhishekagrawal";
char str1[]="askw";
printf("Before %s %s\n",str,str1);
func(str,str1);
printf("After %s \n",str);

cin.get();
return 0;
}

- abhimanipal May 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public List<string> Tokenize(string input, string delimeters)
        {
            HashSet<char> delims = new HashSet<char>(delimeters);
            List<string> strings = new List<string>();
            StringBuilder sb = new StringBuilder();
            foreach (char ch in input)
            {
                if (delims.Contains(ch))
                {
                    if (sb.Length > 0)
                        strings.Add(sb.ToString());
                    sb.Clear();
                }
                else
                    sb.Append(ch);
            }
            if(sb.Length>0) //Required for the case when there is no delimeter at the end of the input string
                strings.Add(sb.ToString());
            return strings;

}
Though I am not super happy about the code and repeat of logic, right now I couldn't find any improvement.

- prolific.coder May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@prolific.coder: More or less the logic would be the same, code is fine.

- cuppanomics May 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

regular expression with delimiter set of sample given, which will return all character groups - can't get easier then this.
([^c,g,j]+)

- BrokenGlass May 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* Assuming that we are dealing with ascii strings
*/

private static List<String> delimit(String str, String delimit){
		
		//Flags to set the delimiters to true 
		boolean[] flag = new boolean[128];
		
		List<String> l = new ArrayList<String>();
		int pivot=0;
		String temp;
		
		for(int i=0 ; i < delimit.length() ; i++){
			flag[(int)delimit.charAt(i)] = true;
		}
		
		for(int i=0; i <str.length() ; i++ ){
			
			if(flag[(int)str.charAt(i)]){
				temp = str.substring(pivot,i);
				 if(temp.length() > 0) {
					 l.add(temp);
				 }
				pivot = i+1;
			}
			
		}
		
		l.add(str.substring(pivot));
		
		return l;
		
	}

- Anonymous June 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* Assuming that we are dealing with ascii strings
*/

private static List<String> delimit(String str, String delimit){
		
		//Flags to set the delimiters to true 
		boolean[] flag = new boolean[128];
		
		List<String> l = new ArrayList<String>();
		int pivot=0;
		String temp;
		
		for(int i=0 ; i < delimit.length() ; i++){
			flag[(int)delimit.charAt(i)] = true;
		}
		
		for(int i=0; i <str.length() ; i++ ){
			
			if(flag[(int)str.charAt(i)]){
				temp = str.substring(pivot,i);
				 if(temp.length() > 0) {
					 l.add(temp);
				 }
				pivot = i+1;
			}
			
		}
		
		l.add(str.substring(pivot));
		
		return l;
		
	}

- Vinoth June 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My answer will goes like this

1. Create the hash map of the delimineters
2. Now scan through the string, if the charater is in the hash map, tokenize that string at that locaction.

Time complexity -> O(m) for creating heap + O(n) scaning the main string

However this approach will work good for ascii characters.

For any kind of string, I think using BST will be better choice

Time complexity -> O(mlogm) for creating heap + O(nlogm) scaning the main string

- manoj gupta June 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There's a big blunder in Vinoth's code. The complexity of this code is O(N^2) because :

The for loop runs n times (where n is the length of string) ... and in each loop iteration Array.Length is computed - which itself takes O(N) time . HENCE, Overall runnig time is O(N^2).

- karanjain2910 July 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

isn't strtok routine?

- netappreject August 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very similar to strtok.
strtok gives one substring for each consecutive call while this one gives all the substrings at once.

- erm October 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

They ask this kind of questions for a PM position?????

- Vet January 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please check below code using hashing .suggestions are always accepted.
void print(char *str,int start,int end)
{
cout<<endl;
while(start<end)
{
cout<<str[start++];
}
}
void deleim_print(char *str,char *del)
{
char hash[256]={0,};
int start=0;
for(int i=0;del[i]!='\0';i++)
hash[del[i]]++;
for(int i=0;str[i];i++)
{
if(hash[str[i]]>=1)
{
print(str,start,i-1);
start=i+1;
}
}
}

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

Is this consider "efficient"? This should be O(n)...

import java.util.ArrayList;

public class SubString {
	public static String subString(String s, ArrayList<Character> deli) {
		String result = "";
		char[] temp = s.toCharArray();

		for (int i = 0; i < temp.length; i++) {
			if (!deli.contains(temp[i])) {
				result += temp[i];
			} else {
				result += ", ";
			}
		}
		return result;
	}

	public static void main(String args[]) {
		ArrayList<Character> arr = new ArrayList<Character>();
		arr.add('c');
		arr.add('g');
		System.out.println(subString("abbcdeffghujsb", arr));
	}
}

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

An easy solution is to replace every occurrence of any delimiter, replace it with ", " (comma + space) and then print the string. Easy!

- Saurabh Mangal November 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bb

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

public string[] DelimitString(string strInput, char[] chDelimChars)
{
string[] strTemp = new string[10];

try
{
foreach (char ch in chDelimChars)
{
strInput = strInput.Replace(ch, '&');
}

strTemp = strInput.Split('&');

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

return strTemp;
}

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

Create a StringBuffer and a set of characters.
We iterate throught the StringBuffer character by character and for every character we check the set.
if at any time we find that the character in StringBuffer is present in the set, we replace that character by a space(' ').
we carray on the above step till we reach the end of the StringBuffer.
Here is the Java code:


import java.util.*;

class Delimiter{
static StringBuffer sbf;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String st = sc.next();
sbf = new StringBuffer(st);
//create Set of the chars
//lets for simplicity I take chars as a d e k you can take it from user or anything
Set<Character> set = new HashSet<Character>();
set.add('a');
set.add('d');
set.add('e');
set.add('k');
for(int i=0;i<st.length();i++)
{
if(set.contains(sbf.charAt(i)))
sbf.setCharAt(i,' ');
}
System.out.println(sbf.toString());
}
}

- Ravi Ranjan March 08, 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