VMWare Inc Interview Question for Member Technical Staffs


Country: United States
Interview Type: Phone Interview




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

public class SortArrays {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a={1,5,7,8,11};
		int[] b={3,7,9,10,23};
		int[] c =new int[a.length+b.length];
		int i=0,j=0,k=0;
		
			while(i<a.length && j<b.length) {		
				if (a[i]<b[j])
				{
					c[k]=a[i];
					k++;
					i++;
				}
				else {
					c[k]=b[j];
					k++;
					j++;
				}
				
		}
			while(i<a.length){
				c[k]=a[i];	
				i++;
			}
			
			while(j<b.length){
				c[k]=b[j];
				j++;
			}
		
for(k=0;k<a.length+b.length;k++)
{
	System.out.println(c[k]);
}
	}

}

- coder July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess, its not for arrays, but linked lists.

- Rishabh July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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
}

- Key July 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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
			}

- Key July 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

//I think you missed to increase k++ in last 2 while loops

- Key July 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- loveCoding July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if both p1 and p2 are null ?
and the input lists p1 and p2 are being modified in your code

- Anonymous July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- shashi_kr August 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use mergesort

- algo September 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

use algo

- mergesort September 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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);

}

- coder September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Rakesh Roy October 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- wolfengineer May 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Suren September 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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


}

- Anonymous September 09, 2015 | 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