## 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??

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

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]

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

``````#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

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

sahil is correct

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].

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

String[] sa = new String[] { "A", "B", "C" };

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

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?

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

@ vjgvnd91 .......Java

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

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

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]

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};
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]);
i=j;
break;
}
}else{
temp=temp+Integer.toString(sa[j]);
i=j;
break;
}
}
}else{
}
}

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

It works.

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)
{
ArrayList internalArr = new ArrayList();
for(int i = 0; i < arr.length; i++)
{

if(l.isEmpty() && i == 0)
{
}
if(i > 0)
{
if(arr[i] - arr[i-1] != 1)
{
internalArr =new ArrayList();
}
else
{
}
}
}

/*
* 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();
}``````

}

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{
s= ""+a[i];

}
last = a[i];
i++;
}
return l;
}``````

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};
List<Integer> nodes = new ArrayList<Integer>();
for (int i = 1 ; i < input.length ; i++) {
if(input[i] == input[i-1] + 1 ) {
continue;
}
else {
int j = outputList.size();
nodes = new ArrayList<Integer>();
j--;
}
}
System.out.println(outputList);

}

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};
List<Integer> nodes = new ArrayList<Integer>();
for (int i = 1 ; i < input.length ; i++) {
if(input[i] == input[i-1] + 1 ) {
continue;
}
else {
int j = outputList.size();
nodes = new ArrayList<Integer>();
j--;
}
}
System.out.println(outputList);

}

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.

``````class LinkedListNode {
String sequeunce;
}

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 };

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());
current.next = temp;
current = temp;
}
}
}
}
}``````

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;
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;
}

else
{
}
}

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++;
}

}
}

}

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;
}

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;
}``````

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.List;

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 };

ArrayList<Integer> nodes = new ArrayList<Integer>();

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

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

continue;

}

else {
int j = outputList.size();
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]]``````

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

It Looks really simple and gud one.

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.

### 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.