Microsoft Interview Question for Applications Developers


Country: India
Interview Type: Written Test




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

check my implementation here

// Find Max Sequence in an unsorted array

#include<iostream>
#include<cstdlib>

using namespace std;

struct sequence_list {
    int *sequence;
    int count;
};

sequence_list* FindMaxSequence(int arr[], int size)
{
    sequence_list *max = new sequence_list;
    max->sequence = NULL;
    max->count = 0;
    sequence_list *temp = new sequence_list;
    temp->sequence = (int*)realloc(temp->sequence, 1);
    *(temp->sequence) = arr[0];
    temp->count = 1;

    for (int i = 1; i < size; i++)
    {
        if (arr[i] == (arr[i-1]+1))
        {
            temp->sequence = (int*)realloc(temp->sequence, ++(temp->count));
            *(temp->sequence + temp->count-1) = arr[i];
        }
        else
        {
            if (temp->count > max->count)
            {
                max->sequence = temp->sequence;
                max->count = temp->count;
            }

            temp->sequence = NULL;
            temp->count = 0;
            temp->sequence = (int*)realloc(temp->sequence, ++(temp->count));
            *(temp->sequence + temp->count-1) = arr[i];

        }
    }

    if (temp->count > max->count)
    {
        max->sequence = temp->sequence;
        max->count = temp->count;
    }

    return max;

}

int main()
{
    int arr[] = {1,2,3,8,9,20,11,12,4,5,6,7};
    sequence_list *list = FindMaxSequence(arr, sizeof(arr)/sizeof(arr[0]));
    cout << endl;
    while(list->count--)
    {
        cout << '\t' << *list->sequence++;
    }
    cout << endl;
    return 0;
}

- AJ November 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include<map.h>
using namespace std;
int max(int x,int y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}

int g=0,h=0,h1=0;

int longest_sequence(int arr[],int index,int length)
{
if(length+1==index)
{
return 0;
}
if(arr[index+1]==arr[index]+1)
{

g++;
h=1+longest_sequence(arr,index+1,length);
return h;
}
else
{
if(g>h1)
{
h1=g;
}
g=0;
return longest_sequence(arr,index+1,length);
}
}

int main(int argc, char *argv[])
{
char * arr[5]={"poi","olk","ert","wer","qwer"};
// int y=rand()%4;
// cout<<arr[y];
int y1[16]={1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 17, 18, 30,31,32};
cout<<longest_sequence(y1,0,15);
cout<<h1+1;



system("PAUSE");
return EXIT_SUCCESS;
}

- shubhajoy.das November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int arr[100];
int max=0, max_in,count;
int longest(int,int);
int main()
{
int i,n;
printf("no of elements = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the elements = ");
scanf("%d",&arr[i]);}
for(i=0;i<n;i++)
{
count = 1;
longest(arr[i],n);}
for(i=0;i<max;i++)
{
printf("%d\t",max_in);
max_in++;
}
getch();
return 0;
}

int longest(int in, int n)
{
int i,in_1=in;
for(i=0;i<n;i++)
{
if(arr[i]==in+1)
{
longest(in+1,n);
count=count+1;
break;
}
}
if(count>max)
{
max=count;
max_in = in_1;
}
return 0;
}

- bkharpuse November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int start_pos, len=0;
void long(int arr[], int n)
{
   int count, s_p, i=0;
   while(i < n)
   {
      count=1;
      if(arr[i+1] == arr[i]+1)
      {
         s_p = i;
         count++;
         for(int j=i+2; j<n-1; j++)
         {
            if(arr[j] != arr[j+1])
               break;
            count++;
         }
      }
      if(count > len)
      {
         len = count;
         start_pos = s_p;
      }
      i = i+count;
   }
}

- vjgvnd91 November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess function should return set of numbers not the max number of elements in the list

- Andi November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sample Input : [1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,31,32]
Sample Output: [13, 14, 15, 16]

- manish.89july November 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i=0;
length=0;
startindex=0;
while(i<size)
{ 
	tempindex=i;
	templength=0;
	do
	{	templength++;
	}while(a[++i]=a[i]+1);
	if(lenght<templength)
	{	length=templength;
		startindex=tempindex;
	}
	
}

- teekay November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You will throw if the array were of length 1 and and you did a[++i]

- ShaQ November 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i=0;
length=0;
startindex=0;
while(i<size)
{ 
	tempindex=i;
	templength=0;
	do
	{	templength++;
	}while(a[++i]=a[i]+1);
	if(lenght<templength)
	{	length=templength;
		startindex=tempindex;
	}
	
}

- teekayansari November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int _tmain(int argc, _TCHAR* argv[])
{
	int num_list[] = {1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 20, 23, 30, 31, 32, 33};
	int max_seq_len = 0;
	int seq_len = 0;
	int num_list_len = sizeof(num_list) / sizeof(int);
	int start_index, longest_seq_start_index = 0;
	int longest_seq_end_index = 0;

	for (int i = 1; i < num_list_len + 1; i++)
	{
		if (num_list[i] == num_list[i-1] + 1)
		{
			if (!seq_len)
				start_index = i - 1;

			seq_len++;
		}
		else
		{
			if (seq_len > max_seq_len)
			{
				max_seq_len = seq_len;
				longest_seq_start_index = start_index;
				longest_seq_end_index = i - 1;
			}

			seq_len = 0;
		}
	}

	printf("longest sequence length is %d\n", max_seq_len+1);
	printf("longest sequence is: [");

	for (int i = longest_seq_start_index; i <= longest_seq_end_index; i++)
	{
		printf("%d ", num_list[i]);
	}
	
	printf("]");

	getchar();

	return 0;
}

- heuristic November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void longestSeq(int arr[], int index, int size)
{
int idx = 0, b = arr[0], sz = 0, mIdx = 0, mSize = 0;
for (int i = 0; i < size, i++)
{
if(arr[i] == b+1)
{
sz++;
}
else{
idx = i;
sz = 1;
}
if (mSize < sz)
{
mIdx = idx;
mSize = sz;
}
}

for(int j = mIdx ; j < mSize; j++)
cout << arr[j] << ' ' ;
}

Please comment if anything is wrong

- praveen November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int longest_sequence(int*, int);
int main()
{
int arr[100], n, i;
printf("no of element do u wnt to enter= ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\nenter the value of a[%d] = ",i);
scanf("%d",&arr[i]);
}
longest_sequence(arr,n);
return 0;
}

int longest_sequence(int *ptr, int total)
{
int max_size=1, temp_start, start, count=1, i;
start = *ptr;
for(i=0; i<total; i++)
{
if(ptr[i] == (ptr[i+1]-1))
{
if(count == 1)
temp_start = ptr[i];
count = count+1;
}
else
{
if(count > max_size)
{
max_size = count;
start = temp_start;
}
count = 1;
}
}
for(i=0; i<max_size; i++)
printf("%d\t",start++);
printf("\n");
return 0;
}

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

import java.util.LinkedHashSet;
import java.util.Set;


public class longestSequence {

	public static void main(String args[])
	{
		int arr[] ={1,2,6,3,4,5,1,2,3,7,8,13,14,15,16,23,1,4,6,7,9};
		Set<Integer> mSet = new LinkedHashSet<Integer>();
		mSet = longSequence(arr);
		System.out.println(" The longest sequence is as follows");
		for(Integer elem: mSet)
		{
			System.out.print(elem+"  ");
		}
	}
	
	public static Set<Integer> longSequence(int arr[])
	{
		Set<Integer> returnSet = new LinkedHashSet<Integer>();
		Set<Integer> storeSet = new LinkedHashSet<Integer>();
		int length=arr.length;
		
		for(int i=0;i<length;i++)
		{   
			storeSet.clear();
			storeSet.add(arr[i]);
			int prev=arr[i];
			for(int j=i+1;j<length;j++)
			{
			if(arr[j]==prev+1)
			{
				storeSet.add(arr[j]);
				prev=arr[j];
				
			}
			else
				
			{  
				if(storeSet.size()>returnSet.size())
				{   
				returnSet.clear();
				returnSet.addAll(storeSet);
				}
				
				break;
			}
			}
		}
		return returnSet;
	}
}

- Sujith November 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is same as previous question with little bit changed logic. Here is tested code in Java.

/*
		Sample Input : [1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,31,32]
		Sample Output: [13, 14, 15, 16]
	*/
	public static void maxSequence(int[] arr)
	{
		ArrayList internalArr = new ArrayList();
		ArrayList maxInternalArr = new ArrayList();
		for(int i = 0; i < arr.length; i++)
		{		
			if(i == 0)
			{
				internalArr.add(arr[i]);
				maxInternalArr.add(arr[i]);
			}
			if(i > 0)
			{
				if(arr[i] - arr[i-1] != 1)
				{
					if(internalArr.size() > maxInternalArr.size())
						maxInternalArr = (ArrayList)internalArr.clone();
					internalArr =new ArrayList();
					internalArr.add(arr[i]);
				}
				else
				{
					internalArr.add(arr[i]);
				}
			}
		}
		if(internalArr.size() > maxInternalArr.size())
			maxInternalArr = (ArrayList)internalArr.clone();
		/*
		 * Print Max sequence
		 */
		for(int i = 0 ; i < maxInternalArr.size(); i++)
		{
			Object ob= maxInternalArr.get(i);
			Integer  printer = new Integer((Integer) ob);
			System.out.print(printer.intValue()+"  ");
			
		}
	}

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

The question is not very clear.. By longest sequence what does it mean.. If you are asking Longest Increasing Subsequence... U can use DP and solve in O(n^2) time and O(n) space complexity. :)

- srikantaggarwal November 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printLongestSequence(int[] arr) {
if(arr == null || arr.length == 0) {
System.out.println("Empty array");
return;
}
String tempStr = arr[0] + ",";
String maxStr = "";
for(int i =1; i< arr.length + 1; i++) {
if(i < arr.length && arr[i] == arr[i-1] + 1) {
tempStr += arr[i] + ",";
} else {
if(tempStr.length() > maxStr.length()) {
maxStr = tempStr;
if(i < arr.length) {
tempStr = arr[i] + ",";
}
}
}
}
System.out.println(maxStr.substring(0, maxStr.length() - 1));
}

public static void main(String[] args) {
printLongestSequence(new int[]{});
printLongestSequence(new int[]{1});
printLongestSequence(new int[]{1,2,3});
printLongestSequence(new int[]{1,2,3,7,8,9,10});
printLongestSequence(new int[]{1,2,3,7,8,9,10,13,14,15,16,17});
}

- zanyaziz November 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

output:
Empty array
1
1,2,3
7,8,9,10
13,14,15,16,17

- zanyaziz November 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] arr = new int[] { 2, 4, 6, 8, 9, 10, 11, 20, 22, 24, 25, 26 };
int start,end;
for (int j = 0; j < arr.Length-1; j++)
{
if (arr[j + 1] == arr[j] + 1)
{
start = j; end = j;
for (int t = start; (t <= end && t <= arr.Length-2); t++)
{
if (arr[t + 1] == arr[t]+1)
{
end = t + 1;
j = end;
}
}

Console.WriteLine("Sequences are");
for (int p = start; p <= end; p++)
{
Console.Write(arr[p]);
}
Console.ReadKey();
}
}

- greenrise123 November 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This Code will print all the sequences....

- greenrise123 November 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static ArrayList<Integer> findMaxSeq(ArrayList<Integer> arr) {
if (arr.size() == 1)
return arr;

int maxlen = 1;
int max = 0;
int flag = 1;

int tmplen = 1;
int tmpmax = 0;
int tmpflag = 1;

int arrlen = arr.size();
int i;
for (i = 1; i < arrlen; ++i) {
if (java.lang.Math.abs(arr.get(i) - arr.get(i - 1)) == 1) {
tmpflag = arr.get(i) - arr.get(i - 1);
tmplen = tmplen + 1;;
tmpmax = arr.get(i);
} else if (tmplen > maxlen) {
flag = tmpflag;
maxlen = tmplen;
max = tmpmax;
tmplen = 0;
}
}

System.out.println("max:" + max + ", maxlen: " + maxlen);

ArrayList<Integer> lst = new ArrayList<Integer>();
for (i = maxlen; i >= 1; i--){
lst.add(max - (i - 1) * flag);
}

return lst;
}

- Zhiguang November 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
* @param args
*/
public static void main(String[] args) {
/*Given an integer array, find the longest sequence and print it.*/
Integer[] input = {1,3,4,5,8,9,11,13,14,15,16,20,23,30,31,32};
List<Integer> segment = new ArrayList<Integer>();
int temp = segment.size();
List<Integer> longestSegment = null;
for (int i = 1 ; i < input.length ; i++) {
segment.add(input[i-1]);
if(input[i] == input[i-1] + 1 ) {
continue;
}
else {
if(temp < segment.size()) {
temp = segment.size();
longestSegment = segment;
}
segment = new ArrayList<Integer>();
}
}
System.out.println(longestSegment);


}

- Rahul Jain November 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void LongestSequence()
{
	int arr[]={1,2,5,6,7,8,10,11,12,14,16,17,18,19,20};
	int count=0,lastsize=0,head=0,lasthead=0;
	for(int i=1;i<sizeof(arr)/sizeof(int);i++)
	{
		if(arr[i-1]==arr[i]-1)
			count++;
		else
		{
			if(count>lastsize)
			{
				lastsize=count;
				lasthead=head;
			}
			count=0;
			head=i;
		}
	}
	if(count>lastsize)
	{
		lastsize=count;
		lasthead=head;
	}
	for(int i=lasthead;i<=lastsize+lasthead;i++)
		cout<<arr[i]<<"-";
	cout<<endl;
}

- Alaa Abouzeid November 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] arr = {1,2,3,4,4,5,6,7,8,9,20,11,12,4,7,10};
		int currsum = 0, sum = 1, end = 0;
		for(int i = 0; i < arr.length; i++)
		{
			currsum = 1;
			while(i != arr.length - 1 && (arr[i] + 1) == (arr[i+1]))
			{				
				currsum++;
				i++;
			}
			if(currsum > sum)
			{
				sum = currsum;
				end = i;
			}
		}
		System.out.println("Max seq: " + sum + " start: " + (end - sum + 1) + " end: " + end);

- Rajeev November 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# include<stdio.h>

void longseq(int *arr, int size)
{
  int i;
  int last= -1; /*The last element scanned from array */
  int max = 0;  /* Max sequence length */
  int pos;    /* For remmembering position where the longest sequence begins */
  int cnt;

  for (i=0; i <size; i++)
  {
    if (last == arr[i]-1)
    {
      cnt++; 
      last = arr[i];
    }
    else
    {
      cnt=1;
      last = arr[i];
    }

    if (max<cnt)
    {
      max = cnt;
      pos = i;
    } 
  }
 
  for (i=pos-cnt;i<=pos;i++)
  printf("%d ", arr[i]);
}
int main()
{
  int arr[] = {1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,31,32};
  longseq(arr, 16);
}

- debjyoti December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

takes O(n) time

- debjyoti December 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int main()
{
	int array[16] = {10,13,1,2,3,7,4,5,6,7,8,9,10,9,1,13},*ptr = array,count = 0,maxcount = 0,i,value = 0,index = 0;
	for(i =0 ; i<16;i++) {
		count = 0;
		if((array[i] +1) == array[i+1]) {
			index = i;
			while((array[i] + 1) == array[i+1]) {
				count++;
				i++;
			}
			if(count > maxcount) {
				maxcount = count;
				value = index;
			}
		}
	}
	for(i = value;i<=value +maxcount;i++) 
		printf("value = %d\n",array[i]);
	return 0;
}

- agmegharaj November 29, 2013 | 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