Microsoft Interview Question
Software Engineer / DevelopersFirst 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;
}
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
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;
}
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;
}
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;
}
bool judgeArray(int* ary, int len, int n){
- lakevista September 23, 2008int 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;
}