## Hewlett Packard Interview Question for Software Engineer / Developers

Team: Networking
Country: United States
Interview Type: In-Person

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

If there are no restrictions, this could be done in O(n).
Assuming the following structure:

``````struct _node
{
struct _node* next;
int data;
};``````

And assuming the interface of the function... This is my solution:

``````void merge(struct _node* h1, struct _node* h2)
{
while (h1->next != NULL)
h1 = h1->next;
h1->next = h2;
}``````

A nice optimization would be to have another structure for the list head, saving the last element as such:

``````struct _head
{
struct _node* first;
struct _node* last;
}``````

Then, an O(1) solution is possible:

``````void merge(struct _head* h1, struct _head* h2)
{
h1->last->next = h2;
h1->last = h2->last
}``````

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

public class Merge {

public Merge() {

}

private int getBiggerSize() {
int result;
int sizeListOne = listOne.size();
int sizeListTwo = listTwo.size();
if(sizeListOne < sizeListTwo) {
result = sizeListTwo;
} else {
result = sizeListOne;
}
return result;
}

public void merge() {
int len = getBiggerSize();
for(int i=0; i<len; i++) {
if(!listOne.isEmpty()) {
listOne.remove();
}
if(!listTwo.isEmpty()) {
listTwo.remove();
}
}
System.out.println("List One: "+ listOne.toString());
System.out.println("List Two: "+ listTwo.toString());
System.out.println("List Merged: "+ result.toString());
}
}

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

``````#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<process.h>
typedef struct node
{
int data;
}node;

node*create()
{
node *start,*last,*temp;
start=NULL;
char choice;
printf("enter choice:");
fflush(stdin);
scanf("%c",&choice);
do
{
temp=(node*)malloc(sizeof(node));
printf("enter data");
scanf("%d",&(temp->data));
if(temp==NULL)
printf("not enough memory to create node");
else
{
if(start==NULL)
{
start=temp;
last=start;
}
else
{
last=temp;
}
}
printf("enter choice:");
fflush(stdin);
scanf("%c",&choice);

}while(choice=='y');
if(start!=NULL)
return(start);
}
void print(node*start)
{
node*temp;
printf("%d\t",temp->data);
}
node*convert(node*x,node*y)
{
node*z,*temp;
z=x;
temp=z;
while(x!=NULL&&y!=NULL)
{
temp=y;
temp=x;
}
while(x!=NULL)
{
x=NULL;
}
while(y!=NULL)
{
y=NULL;
}
return z;
}
int main()
{

node*x,*y,*z;
printf("1st ll");
x=create();
printf("2nd ll");
y=create();
z=convert(x,y);
print(z);

}``````

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

``````public static void merge2LinkedLists(LinkedList<String> list1, LinkedList<String> list2){
int size = Math.max(list1.size(), list2.size());
while(size > 0) {
if(!list1.isEmpty()){
}
if(!list2.isEmpty()) {
}
size--;
}

System.out.println(result.toString());

}``````

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.