Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

public static String replaceOld(final String aInput, final String aOldPattern, final String aNewPattern) {
if (aOldPattern.equals("")) {
throw new IllegalArgumentException("Old pattern must have content.");
}

final StringBuffer result = new StringBuffer();
// startIdx and idxOld delimit various chunks of aInput; these
// chunks always end where aOldPattern begins
int startIdx = 0;
int idxOld = 0;
while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) {
// grab a part of aInput which does not include aOldPattern
result.append(aInput.substring(startIdx, idxOld));
// add aNewPattern to take place of aOldPattern
result.append(aNewPattern);

// reset the startIdx to just after the current match, to see
// if there are any further matches
startIdx = idxOld + aOldPattern.length();
}
// the final chunk will go to the end of aInput
result.append(aInput.substring(startIdx));
return result.toString();
}

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

public static String replaceOld(final String aInput, final String aOldPattern, final String aNewPattern) {
if (aOldPattern.equals("")) {
throw new IllegalArgumentException("Old pattern must have content.");
}

final StringBuffer result = new StringBuffer();
// startIdx and idxOld delimit various chunks of aInput; these
// chunks always end where aOldPattern begins
int startIdx = 0;
int idxOld = 0;
while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) {
// grab a part of aInput which does not include aOldPattern
result.append(aInput.substring(startIdx, idxOld));
// add aNewPattern to take place of aOldPattern
result.append(aNewPattern);

// reset the startIdx to just after the current match, to see
// if there are any further matches
startIdx = idxOld + aOldPattern.length();
}
// the final chunk will go to the end of aInput
result.append(aInput.substring(startIdx));
return result.toString();
}

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

//to find out the index of the occurances of the substring in the original String
//This list stores the starting index of subString incase it is present multiple times

private static List listOfIndecesOfSubstring(String label, String subString) {

int i = 0;
for (i = 0; i < label.length(); i++) {

int j = 0;

if (label.charAt(i) == subString.charAt(j)) {
int k = i + 1;
j++;

for (; j < subString.length(); j++,k++) {
if (label.charAt(k) != subString.charAt(j)) {
break;
}
}

if (subString.length() == j) {
mIndex.add(String.valueOf(i));
}
}

}
mIndex.trimToSize();
return mIndex;
}

//to replace the substring with newString
private static String replaceAll(char[] actual_sentence,char[] arrayToBeReplaced, char[] newArray){

char[] final_sentence = new char[actual_sentence.length + (newArray.length - arrayToBeReplaced.length)*mIndex.size()];
int arrayListIndex = 0;
int k = 0;
for(int i=0;i< actual_sentence.length;i++,k++){

int occurAtIndex = Integer.valueOf((String)mIndex.get(arrayListIndex));

if(i != occurAtIndex){
final_sentence[k]=actual_sentence[i];
}else{
for(int j = 0;j < newArray.length;j++,k++){
final_sentence[k]= newArray[j];
}
arrayListIndex++;
i=i+arrayToBeReplaced.length -1;
k=k-1;
}

}
return new String(final_sentence);
}

- sid_tintin May 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public StringBuilder replacedString(StringBuilder text, String pattern, String replacementText) {
		int index = text.indexOf(pattern);
		while(index != -1) {
			text.replace(index, index + pattern.length(), replacementText);
			index = index + replacementText.length();
			index = text.indexOf(pattern);
		}
		return text ;
	}

- anon June 13, 2012 | Flag


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