Microsoft Interview Question for Software Engineer / Developers






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

bool judgeArray(int* ary, int len, int n){
int count = 1;
if(len < n) {
return false;
} else if (n == 0) {
return true;
} else if (len > 0 && n == 1)
return true;
for(int i = 1; i < len; i++){
if(ary[i] == ary[i-1]) {
count ++;
if(count == n)
return true;
} else count = 1;
}
return false;
}

- lakevista September 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

looks bulletproof. null check for ary can be added for more robustness..

- acoder September 27, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

First solution:
///
<summary>
/// This function will return true or false.
/// If function finds the sequence of any integer >= provided n, it will return true else false
/// </summary>
/// <param name="arr"></param>
/// <param name="n"></param>
/// <returns></returns>
/// <example>
/// If n=3, a={1,1,2,2,3,3,3,4,5} return true as integer 3 are 3 times in sequence
/// If n=3, a={1,2,2,3,3,4,4,4,5} return false as no integer are 3 times in sequence
/// If n=4, a={1,2,2,2,2,3,3,3,5} return true as integer 2 are 4 times in sequence
/// </example>
public static bool FindSequence (int[] arr, int n)
{
int count = 1;
int firstOccurance = arr[0];
if (n == 0) return true; // Not looking for any occurance, so return true
if (arr.Length < n) return false; // Sequnce count is > lenght, return false
if (arr.Length == 1) return true; // Array length is 1 and n = 1 also, return 1
for (int i = 1; i < arr.Length; i++)
{
if (count < n)
{
if (firstOccurance == arr[i])
{
count++;
}
else
{
firstOccurance = arr[i];
count = 1;
}
}
if (count == n)
{
return true;
}
}
return false;
}


Second solution:
///
<summary>
/// Write a function which determines, wheather the array is a mirror - reflection - like.
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
/// <example>
/// input[] = {1,2,3,2,1} gives true
/// input[] = {1,2,3,3,2,1} gives true.
/// input[] = {1,2,4,3,2,1} gives false.
/// </example>
public static bool MirrorReflection(int[] arr)
{
int start = 0;
int end = arr.Length - 1;
while (end > start)
{
if (arr[start] != arr[end])
{
return false;
}
start++;
end--;
}
return true;
}

- Master September 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)sort the array, then check if any exists. O(nlogn)
2)easy

- Anonymous September 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorting will give u false result . Question says there is a sequence of n or more elements which have same values means the order they come in the array shld have same value

- backtolife September 24, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good observation. In this case, the question is even easier.

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

Java:

public boolean sequenceTest(int[] arr, int num){
int temp;
int count = 1;
for(int i = 0; i < arr.length; i++){
if((i+1 < arr.length) && (arr[i] == arr[i+1])){
count++;
} else if(count == 3){
return true;
}else {
count = 1;
}
}

return count == 3;
}

public boolean isMirror(int[] arr){
int length = arr.length;
int mid = length /2;

if(length%2 != 0)
mid++;

for(int i = 0, j = length - 1; i < mid; i++, j--){
if(arr[i] != arr[j])
return false;
}

return true;
}

- MildGeek September 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java, passed the given examples.

public static void main(String[] args) {
// TODO Auto-generated method stub
int [] ir = new int [] {5,2,2,3,2,5,5};

if(sequenceTest(ir, 3))
{
System.out.println("yes");
}
else
{
System.out.println("no");
}
}



static boolean sequenceTest(int[] ir, int len)
{
if (ir.length == 0)
return false;

else if ((len == 0) || (len == 1))
return true;

else if ( ir.length < len)
return false; //definition

else
{
for (int i = len-1; i < ir.length; i++)
{
boolean flag = true;

for (int j = len; j > 1; j--)
{
int base = i;
int baseM = base - j + 1;
int baseMM = base - j + 2;

if (ir[baseM] < ir[baseMM])
{

}
else
{
flag = false;
}
}

if (flag)
{
return true;
}
}



}
return false;

}

- BananaOrange October 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There might be mistakes in above programs.
Here is a working one in C++:

bool repeat(int arr[], int length, int n)
{
if (length <=0) return false;
if (n >length) return false; // not enough numbers
if (n==1) return true; // no need to check
int start = 0; // start from first
while (start < length)
{
int exam = arr[start]; // check next non-repeated number
int i= 1;
while (i< n)
{
if (arr[start+i]== exam)
{
i++;
cout << i << endl;
if (i==n) // find one
{
cout << exam << " repeat " << n << " !"<<endl;
return true;
}
}
else
{
cout << exam << " repeat only " << i << endl;
start += i;
exam = arr[start];
i=1;
}


if (length - start < n)
{
cout << "not enough to go on" << endl;
return false;
}
}
}
return false;
}

- kulang November 09, 2008 | 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