Google Interview Question
1,230 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Write a function to modify a singly linked list, appending copies of the first N-1 elements to the end in reverse order. For example, ['A', 'B', 'C', 'D'] becomes ['A', 'B', 'C', 'D', 'C', 'B', 'A'].
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (10)
0 of 1 people found this helpful
this would be more memory efficient. remember to think google scale for google interview questions.
char[] appendReverse(char[] in)
{
int n = in.length;
char[] out = new char[n2-1];
for(int i = 0; i < n; i++)
{
out[i] = in[i];
out[n-i] = in[i];
}
return out;
}
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
8 of 9 people found this helpful
'D' :A', 'B', 'C', 'D' 'C' 'B' 'A'
Helpful Answer?
Yes |
No
Inappropriate?
4 of 4 people found this helpful
struct List {
List * next;
char val;
};
void modify( List * node ) {
if (node == NULL)
return;
List * rev = NULL ;
while (node->next != NULL) {
List * newNode = new List;
newNode->next = rev;
newNode->val = node->val;
rev = newNode;
node = node->next;
}
node->next = rev;
}
Helpful Answer?
Yes |
No
Inappropriate?
void modify (Node * node) {
Node * last = node;
while (last->next != NULL) {
last = last->next;
}
Node * curr = node;
while (curr != last) {
Node * duplicate = new Node;
duplicate->key = curr->key;
duplicate->next = last->next;
last->next = duplicate;
curr = curr->next;
}
}
Helpful Answer?
Yes |
No
Inappropriate?
2 of 2 people found this helpful
void foo(node *p)
{
if(p->next == 0)
return p;
node * end = foo(p->next);
end->next = p->copy();
return end->next;
}
Helpful Answer?
Yes |
No
Inappropriate?
def modify(root):
s = root
last = node(root.v)
while root.n.n:
root = root.n
n = node(root.v)
n.n = last
last = n
root.n.n = last
return s
Helpful Answer?
Yes |
No
Inappropriate?
Node createList(Node n) {
if(n.next == null) return n;
else {
Node n1= createList(n.next);
Node n2 =new Node( n.value);
n2.next = null;
n1.next = n2;
return n2;
}
}
Helpful Answer?
Yes |
No
Inappropriate?
@MB Assuming you have a pointer to the end of the list means you already went through the list once. Then you have to append n-1 elements at the end of it, so in the end it takes twice the time.
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
1 of 2 people found this helpful
by Using stacks: