Microsoft Interview Question for Applications Developers


Country: India
Interview Type: Written Test




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

wat do you mean by containing one sequence??

- sahil.bathla1 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 Linked List : [1] -> [3,4,5] -> [8,9] -> [11] -> [13,14,15,16]->[20]->[23]->[30,31,32]

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

#include <iostream>

using namespace std;

struct list
{
       char *value;
       list * next;
}*first=NULL;
  
  
  void JoinList(int A[],int size)
  {
       char *tempString = new char(15);                 
       int i=0;list *currentNode;
       while(i<size)
       {
       list *temp=new list;
       temp->value=new char(15);
       
       strcpy(temp->value,"");
       temp->next=NULL;
       
       do
       {
       itoa(A[i],tempString,10);                                //convert array element to string
       strcat(temp->value,tempString);                          //concatenate to current node
       i++;
       if(i>=size)
       break;
       }while(A[i]==(A[i-1]+1));                                // till the next element is one greater than the current element
       
       if(first==NULL)
                      {
                     first=temp;
                     currentNode=temp;
                     }
       else
                     {
                     currentNode->next=temp;                   
                     currentNode=temp;
                     }                         
       }
   }
   
   void display(list * node)
   {
        while(node!=NULL)
        {cout<<node->value<<" ";
        node=node->next;
        }
   }
 int main()
 {
     int A[12]= {2,3,4,5,6,9,10,12,13};
     JoinList(A,9);
     display(first);
     system("pause");
     return 0;
     }

Steps :-
1) convert current array element to string & add to list
2) read & concatenate further elements provided the next element = previous element + 1
3) repeat 1 & 2 to form the linked list till you run out of array elements

Just see the comments in Join List Function. You will understand

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

sahil is correct

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

Hi

If you observe the input there are some numbers followed by consecutive numbers. For eg:

1 does not have consecutive number 2. So, add 1 in Linked list node.
Add next element in input, i.e 3 to another node of Linked List, then check in the input array for 3 consecutive, i.e 4, it is available. So, add 4 to the same node of Linked list. Like that 5 also to the same node of Linked List.

Now, the linked list contains the [1]->[3,4,5]

Next element in 8 not consecutive to 5. So, add 8 to another node of Linked list.

Like that will have to create Linked List.

Hope you understand the sequence.

Final Sequence is : [1 ]-> [3,4,5]-> [8,9] -> [11] -> [13,14,15,16] -> [20] -> [23] -> [30,31,32].

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

String[] sa = new String[] { "A", "B", "C" };
LinkedList ll = new LinkedList(Arrays.asList(sa));

// iterate over each element in LinkedList
Iterator iterator = ll.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}

Hoping that it will help you.

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

I have a question. Does the code have to be in C# / Java or is C, C++ acceptable?

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

@ vjgvnd91 .......Java

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

I didn't get the question. It is always a good idea to add an example use case with the question

- inasaa 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 Linked List : [1] -> [3,4,5] -> [8,9] -> [11] -> [13,14,15,16]->[20]->[23]->[30,31,32]

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

Integer[] sa ={1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,31,32};
LinkedList aa = new LinkedList();
aa.addFirst(sa[0]);
for(int i=1;i<sa.length-1;i++){
if(sa[i-1]+1==sa[i]){
aa.removeLast();
String temp="";
for(int j=i-1;j<sa.length-1;j++){
int a=sa[j];
int b=sa[j+1];
if(a+1==b){
temp=temp+Integer.toString(sa[j])+":";
if(j==sa.length-2){
temp=temp+Integer.toString(sa[j+1]);
aa.addLast(temp);
i=j;
break;
}
}else{
temp=temp+Integer.toString(sa[j]);
aa.addLast(temp);
i=j;
break;
}
}
}else{
aa.addLast(sa[i]);
}
}

Iterator iterator = aa.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.print(iterator.next()+" ");
}

It works.

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

This is tested code in Java

/*
	 * Input array = [1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30, 31 32]
	 * Output = [1]->[3,4,5]->[8,9]->[11]->[13,14,15,16]->[20]->[23]->[30,31,32]
	 */
	public static void arrangeList(int[] arr)
	{
		List<Object[]> l = new LinkedList<Object[]>();
		ArrayList internalArr = new ArrayList();
		for(int i = 0; i < arr.length; i++)
		{
			
			if(l.isEmpty() && i == 0)
			{
				internalArr.add(arr[i]);
			}
			if(i > 0)
			{
				if(arr[i] - arr[i-1] != 1)
				{
					l.add(internalArr.toArray());
					internalArr =new ArrayList();
					internalArr.add(arr[i]);
				}
				else
				{
					internalArr.add(arr[i]);
				}
			}
		}
		l.add(internalArr.toArray());
		
		/*
		 * Print Output
		 */
		for(int i = 0 ; i < l.size(); i++)
		{
			Object[] objArr = l.get(i);
			int count = 0;
			for(Object ob : objArr)
			{
				if(count != 0)
					System.out.print("->");
				Integer  printer = new Integer((Integer) ob);
				System.out.print(printer.intValue());
				count++;
				
			}
			System.out.println();
		}

}

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

public static List<String> continousInt(int a[]){
		
		if(a.length == 0)
			 return null;
		List <String > l = new LinkedList<String>();
		
		int i = 0;
		String s = ""+a[i];
		int last = a[i];
		i++;
		while(i<a.length){
			if(last+1 ==a[i]){
				s = s +a[i];
				
			}else{
				l.add(s);
				s= ""+a[i];
				
			}
			last = a[i];
			i++;
		}
		l.add(s);
		return l;
	}

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

public static void main(String[] args) {
Integer[] input = {1,3,4,5,8,9,11,13,14,15,16,20,23,30,31,32};
LinkedList outputList = new LinkedList();
List<Integer> nodes = new ArrayList<Integer>();
for (int i = 1 ; i < input.length ; i++) {
nodes.add(input[i-1]);
if(input[i] == input[i-1] + 1 ) {
continue;
}
else {
int j = outputList.size();
outputList.add(j,nodes);
nodes = new ArrayList<Integer>();
j--;
}
}
System.out.println(outputList);

}

- This is giving the correct output November 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
Integer[] input = {1,3,4,5,8,9,11,13,14,15,16,20,23,30,31,32};
LinkedList outputList = new LinkedList();
List<Integer> nodes = new ArrayList<Integer>();
for (int i = 1 ; i < input.length ; i++) {
nodes.add(input[i-1]);
if(input[i] == input[i-1] + 1 ) {
continue;
}
else {
int j = outputList.size();
outputList.add(j,nodes);
nodes = new ArrayList<Integer>();
j--;
}
}
System.out.println(outputList);

}

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

Hi please find the java code to convert the input array into a linkedlist each node containing a sequence.

Please reply if you find any mistakes .

class LinkedListNode {
	String sequeunce;
	LinkedListNode next;
}

public class IntegerArrayToSequenceList {

	public static void main(String[] args) {

		int[] inputArray = { 1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,
				31, 32 };

		LinkedListNode head = null;
		LinkedListNode current = new LinkedListNode();
		head = current;

		StringBuffer strBuf = new StringBuffer();

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

			if (i + 1 < inputArray.length
					&& inputArray[i] == inputArray[i + 1] - 1)
				strBuf.append(inputArray[i] + ",");
			else {
				strBuf.append(inputArray[i]);
				current.sequeunce = strBuf.toString();
				strBuf.delete(0, strBuf.length());
				LinkedListNode temp = new LinkedListNode();
				current.next = temp;
				current = temp;
			}
		}
		while (head != null) {
			if (head.sequeunce != null)
				System.out.println(head.sequeunce);
			head = head.next;
		}
	}
}

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

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int a[]= {1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,31,32};

struct node
{
char data[10];
struct node *next;
};

struct node* createnode(int x)
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
char s[10];
itoa(x,s,10);
strcpy(temp->data,s);
temp->next=NULL;

return temp;
}

void insert(struct node**p)
{
int i;
struct node *temp,*link;
char s1[10];
for(i=0;i<16;i++)
{
if(a[i]+1!=a[i+1])
{
temp=createnode(a[i]);
if(*p==NULL)
{
*p=temp;
link=temp;
}

else
{
link->next=temp;
link=link->next;
}
}

else
{
temp=createnode(a[i]);
while(a[i]+1==a[i+1])
{
itoa(a[i+1],s1,10);
strcat(temp->data,",");
strcat(temp->data,s1);

i++;
}

link->next=temp;
link=link->next;
}
}


}

void show(struct node *p)
{
while(p!=NULL)
{
printf("\n%s",p->data);
p=p->next;
}
}


int main()
{
int i;
struct node *p=NULL;

insert(&p);
show(p);
getch();
return 0;
}

- puneet kumar November 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is tested code in C++, making use of the STL.

#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
struct sequence {
    int start_num;
    int sequence_length;
};
void sequencify(int *input,int length,list<sequence> &first_seq) {
    list<sequence> retval;
    if (input == NULL || length < 1) {
        return;
    }
    sequence s;
    s.start_num = *input;
    s.sequence_length = 1;
    first_seq.push_back(s);
    for (int *i = input + 1;i < input + length;i++) {
        if (*(i - 1) == (*i) - 1) {
            first_seq.back().sequence_length++;
        }
        else {
            sequence s;
            s.start_num = *i;
            s.sequence_length = 1;
            first_seq.push_back(s);
        }
    }
}

int main(int argc, const char * argv[])
{
    vector<int> vecint;
    for (int i = 0;i < 100;i++) {
        vecint.push_back((int) rand()%100);
    }
    sort(vecint.begin(),vecint.end());
    for (int j : vecint) {
        cout << j << " ";
    }
    cout << endl;
    list<sequence> result;
    sequencify( vecint.data(), vecint.size(), result);
    for (auto j : result) {
        cout << "Start: " << j.start_num << " Length " <<  j.sequence_length << "\n";
    }
    return 0;
}

- Dr. Andrew December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try this one......Above code are not correct (posted by Rahul)....That's not printing last sequence of Numbers

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class IntArrayToLinkedListSequence {

	public static void main(String[] args) {
		int[] input = { 1, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,
				31, 32, 45, 66, 58, 98, 45, 23, 24, 25, 55, 56 };
		
		List<List> outputList = new LinkedList<List>();
		ArrayList<Integer> nodes = new ArrayList<Integer>();

		for (int i = 0; i < input.length; i++) {
			nodes.add(input[i]);

			if (i + 1 < input.length && input[i] == input[i + 1] - 1) {

				continue;

			}

			else {
				int j = outputList.size();
				outputList.add(j, nodes);
				nodes = new ArrayList<Integer>();

			}
		}

		System.out.println(outputList);

	}

}


Output:

[[1], [3, 4, 5, 6], [8, 9], [11], [13, 14, 15, 16], [20], [23], [30, 31, 32], [45], [66], [58], [98], [45], [23, 24, 25], [55, 56]]

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

It Looks really simple and gud one.

- vijay July 19, 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