VMWare Inc Interview Question
Member Technical StaffsCountry: United States
Interview Type: Phone Interview
while(i<a.length){
c[k]=a[i];
i++;
//I think you missed to increase k++ here
}
while(j<b.length){
c[k]=b[j];
j++;
//I think you missed to increase k++ here
}
while(i<a.length){
c[k]=a[i];
i++;
//I think you missed to increase k++ here
}
while(j<b.length){
c[k]=b[j];
j++;
//I think you missed to increase k++ here
}
void sort(Node p1, Node p2) {
if (p1 == null)
return p2;
if (p2 == null)
return p1;
Node res = new Node(p1.data < p2.data ? p1.data : p2.data);
if (p1.data < p2.data)
p1 = p1.next;
else
p2 = p2.next;
Node endNode = res;
while (p1 != null && p2 != null) {
if (p1.data < p2.data) {
endNode.next = p1;
endNode = endNode.next;
p1 = p1.next;
endNode.next = null;
} else {
endNode.next = p2;
endNode = endNode.next;
p2 = p2.next;
endNode.next = null;
}
}
if (p1 == null)
endNode.next = p2;
else if (p2 == null)
endNode.next = p1;
return res;
}
public class SortTwoSortedArr {
public static void main(String[] args){
int[] A = { 1, 3, 5, 8, 10, 14, 19, 23, 26, 28, 51, 52 };
int la = A.length;
int[] B = { 2, 4, 7, 8, 11, 13, 18, 20, 23, 26, 30, 31};
int lb = B.length;
mergeArray(A, la, B, lb);
}
static void mergeArray(int []A, int la, int []B, int lb){
int dubCount=0;
for(int i=0; i<la; i++)
for(int j=0; j<la; j++)
if(A[i]==B[j])
dubCount++;
int dc=la+lb-dubCount;
int [] C=new int [dc];
int a=0, b=0, k=0;
while(a<la && b<lb){
C[k++]=min(A[a], B[b]);
if(A[a]<B[b])
a++;
else if(A[a]>B[b])
b++;
else if(A[a]==B[b]){
a++;
b++;
}
}
while(a<la){
C[k++]=A[a];
a++;
}
while(b<lb){
C[k++]=B[b];
b++;
}
for(int i=0; i<dc; i++)
System.out.print(C[i]+" ");
}
static int min(int a, int b){
return a<b?a:b;
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int val;
struct node *next;
};
struct node* createlist()
{
struct node *head,*temp,*prev;
printf("enter the values");
int j=1;
head=malloc(sizeof(struct node));
scanf("%d",&head->val);
head->next=NULL;
j=head->val;
prev=head;
while(1)
{
scanf("%d",&j);
if(j==0)
break;
prev->next=malloc(sizeof(struct node));
temp=prev->next;
temp->val=j;
temp->next=NULL;
prev->next=temp;
prev=temp;
}
return head;
}
void display(struct node *head)
{
while(head->next!=NULL)
{
printf("%d->",head->val);
head=head->next;
}
printf("%d",head->val);
printf("\n");
}
struct node * merge(struct node *head1,struct node *head2)
{
struct node *head,*temp,*pre;
if(head1->val<head2->val)
{
head=head1;
head1=head1->next;
}
else
{
head=head2;
head2=head2->next;
}
pre=head;
//printf("hi");
while(1)
{
//printf("one");
if(head1!=NULL && head2!= NULL)
{
if(head1->val<head2->val)
{
pre->next=head1;
pre=head1;
head1=head1->next;
pre->next=NULL;
}
else
{
pre->next=head2;
pre=head2;
head2=head2->next;
pre->next=NULL;
}
}
if(head1==NULL)
{
while(head2!=NULL)
{
pre->next=head2;
pre=head2;
head2=head2->next;
}
pre->next=NULL;
break;
}
if(head2==NULL)
{
while(head1!=NULL)
{
pre->next=head1;
pre=head1;
head1=head1->next;
}
pre->next=NULL;
break;
}
//printf("%d %d %d",temp->val,head1->val,head2->val);
}
return head;
}
int main()
{
struct node *head1,*head2,*head;
head1=createlist();
display(head1);
head2=createlist();
display(head2);
head=merge(head1,head2);
printf("\nmerged list is\n");
display(head);
}
I have my LinkedList class whose data is int type.
public class LinkedList {
private int data;
private LinkedList next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public LinkedList getNext() {
return next;
}
public void setNext(LinkedList next) {
this.next = next;
}
}
public static LinkedList mergeListsIterative(LinkedList a, LinkedList b){
LinkedList head = null;
if(a == null)
return b;
else if(b == null)
return a;
if( a.getData() <= b.getData()){
head = a;
}else{
head = b;
b = a;
a= head;
}
LinkedList temp;
while((a.getNext() != null) && b != null){
if(a.getNext().getData() <= b.getData()){
a = a.getNext();
}else{
temp = a.getNext();
a.setNext(b);
b = temp;
}
}
if(a.getNext() == null){
a.setNext(b);
}
return head;
}
Function 2 merge 2 lists.
public static Node mergenode(Node head1, Node head2)
{
if(head1==null && head2==null) return null;
if(head1==null)return head2;
if(head2==null) return head1;
if(head1.value<head2.value)
{
head1.next=mergenode(head1.next,head2);
return head1;
}
else
{
head2.next=mergenode(head2.next,head1);
return head2;
}
}
struct node * merge_two_sorted_single_LL(struct node * l1, struct node * l2) {
if (!l1) {
return l2;
} else if {
return l1;
} else {
if (l1->data < l2-data) {
struct node * p1 = l1;
while (p1->next && p1->data < l2->data
&& p1->next->data < l2-data)
p1 = p1->next;
struct node * q1 = p1->next;
p1->next = l2;
l2->next = merge_two_sorted_single_LL(q1, l2->next);
return l1;
} else {
struct node * p2 = l2;
while (p2->next && p2->data < l1->data
&& p2->next->data < l1-data)
p2 = p2->next;
struct node * q2 = p2->next;
p2->next = l1;
l1->next = merge_two_sorted_single_LL(q2, l1->next);
return l2;
}
}
}
public class SortList {
public static void main(String[] args) {
ArrayList l1=new ArrayList();
l1.add(10);
l1.add(20);
l1.add(30);
l1.add(40);
ArrayList l2= new ArrayList();
l2.add(11);
l2.add(12);
l2.add(14);
ArrayList l3 = new ArrayList();
l3.addAll(l1);
l3.addAll(l2);
Collections.sort(l3);
for(int i=0;i<l3.size();i++)
{
System.out.println(l3.get(i));
}
}
}
- coder July 23, 2013