Google Interview Question for Software Engineers


Country: United States




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

what?

- Anonymous January 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

This was not a trivial problem in implementation :-)

#include <stdio.h>
#include <stdlib.h>

void
print_array(int *arr, int size)
{
	int i;

	for(i=0; i<size; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}

/* Byte-wise swap two items of size SIZE. */
#define SWAP(a, b, size)                      \
do                                            \
{                                             \
	register size_t __size = (size);          \
	register char *__a = (a), *__b = (b);     \
	do                                        \
	{                                         \
		char __tmp = *__a;                    \
		*__a++ = *__b;                        \
		*__b++ = __tmp;                       \
	} while (--__size > 0);                   \
} while (0)

/* Byte-wise set one item of size SIZE. */
#define SET(a, b, size)                       \
do                                            \
{                                             \
	register size_t __size = (size);          \
	register char *__a = (a), *__b = (b);     \
	do                                        \
	{                                         \
		*__a++ = *__b++;                      \
	} while (--__size > 0);                   \
} while (0)

void
insertion_sort(void *ptr, int nitems, int item_size, int (*functionp)(const void *, const void *)) 
{
	int i=0;
	char *val, backup_val;
	int j;
	char *arg1, *arg2, *arg3;
	char *charPtr = (char *)ptr;

	for(j=0; j<nitems-1; j++) {			// 3 4 2 1....2 3 4 1.....1 2 3 4
		
		// Move last element of one sub-array to its final position.
		i = j+1;
		val = (charPtr + (i*item_size));
		SET(&backup_val, val, item_size);
		i--;

		for(i; i>=0; i--) {
			arg1 = (charPtr + (i*item_size));
			arg2 = &backup_val;
			if (functionp((void *)arg1, (void *)arg2) == 1) {
				arg3 = (charPtr + ((i+1)*item_size));
				SET(arg3, arg1, item_size);			// IMP: SET(dst, src)
			} else {
				break;
			}
		}
		arg1 = charPtr + ((i+1)*item_size);
		arg2 = &backup_val;
		SET(arg1, arg2, item_size); // found final place for one element
	}
}			
			

int 
compare_ints(const void *x, const void *y)
{
	int *a, *b;

	a = (int *)x;
	b = (int *)y;

	if (*a < *b) {
		return (-1);
	} else if (*a > *b) {
		return (1);
	} else {
		return (0);
	}
}

void 
GoogleSort(void *ptr, int nitems, int item_size, int (*functionp)(const void *, const void *)) 
{ 
	insertion_sort(ptr, nitems, item_size, functionp);			
}

int
main(void)
{
	int arr[] = {4, 3, 2, 1};

	printf("Array contents before sorting:\n");
	print_array(arr, sizeof(arr)/sizeof(int));

	GoogleSort(arr, sizeof(arr)/sizeof(int), sizeof(int), compare_ints);

	printf("Array contents after sorting:\n");
	print_array(arr, sizeof(arr)/sizeof(int));

	return (0);
}

- smallchallenges January 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@smallchallenges: This is the output:
Array contents before sorting:
4 3 2 1
Array contents after sorting:
-255 3 2 1

- tefposluszny February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

OK, I debugged it. Was fun to do with GDB. The function insertion_sort() where setting backup_val on its stack ;) Here is a fixed version:

#include <stdio.h>
#include <stdlib.h>

void
print_array(int *arr, int size)
{
	int i;

	for(i=0; i<size; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}

/* Byte-wise swap two items of size SIZE. */
#define SWAP(a, b, size)                      \
do                                            \
{                                             \
	register size_t __size = (size);          \
	register char *__a = (a), *__b = (b);     \
	do                                        \
	{                                         \
		char __tmp = *__a;                    \
		*__a++ = *__b;                        \
		*__b++ = __tmp;                       \
	} while (--__size > 0);                   \
} while (0)

/* Byte-wise set one item of size SIZE. */
#define SET(a, b, size)                       \
do                                            \
{                                             \
	register size_t __size = (size);          \
	register char *__a = (a), *__b = (b);     \
	do                                        \
	{                                         \
		*(__a++) = *(__b++);                      \
	} while (--__size > 0);                   \
} while (0)

void
insertion_sort(void *ptr, int nitems, int item_size, int (*functionp)(const void *, const void *)) 
{
	int i=0;
	char *val, *backup_val = (char*)malloc(item_size);
	int j;
	char *arg1, *arg2, *arg3;
	char *charPtr = (char *)ptr;

	for(j=0; j<nitems-1; j++) {			// 3 4 2 1....2 3 4 1.....1 2 3 4
		
		// Move last element of one sub-array to its final position.
		i = j+1;
		val = (charPtr + (i*item_size));
		SET(backup_val, val, item_size);
		i--;

		for(; i>=0; i--) {
			arg1 = (charPtr + (i*item_size));
			arg2 = backup_val;
			if (functionp((void *)arg1, (void *)arg2) == 1) {
				arg3 = (charPtr + ((i+1)*item_size));
				SET(arg3, arg1, item_size);			// IMP: SET(dst, src)
			} else {
				break;
			}
		}
		arg1 = charPtr + ((i+1)*item_size);
		arg2 = backup_val;
		SET(arg1, arg2, item_size); // found final place for one element
	}
        free(backup_val);
}			
			

int 
compare_ints(const void *x, const void *y)
{
	int *a, *b;

	a = (int *)x;
	b = (int *)y;

	if (*a < *b) {
		return (-1);
	} else if (*a > *b) {
		return (1);
	} else {
		return (0);
	}
}

void 
GoogleSort(void *ptr, int nitems, int item_size, int (*functionp)(const void *, const void *)) 
{ 
	insertion_sort(ptr, nitems, item_size, functionp);			
}

int
main(void)
{
	int arr[] = {4, 3, 2, 1};

	printf("Array contents before sorting:\n");
	print_array(arr, sizeof(arr)/sizeof(int));

	GoogleSort(arr, sizeof(arr)/sizeof(int), sizeof(int), compare_ints);

	printf("Array contents after sorting:\n");
	print_array(arr, sizeof(arr)/sizeof(int));

	return (0);
}

- tefposluszny February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Test

- JoseCuervo April 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

#include <iostream>

using namespace std;

void GoogleSort(void *ptr, int number, int SIZE, int (*functionp)(const void *, const void *))

{

for( int n = 0; n < number; n++ )

{

for(int m = n; m < number; m++)

{

if( functionp( (const char *)(ptr) + (n * SIZE), (const char *)(ptr) + (m * SIZE) ))

{

int temp = *((int *)ptr + m);

*((int*)ptr + m ) = *((int*)ptr + n );

*((int*)ptr + n ) = temp;

}

}

}

}

int compare(const void *a, const void *b)

{

if( *(int*)a > *(int*)b)

return 1;

else

return 0;

}

int main()

{

int arr [] = {2,5,7,9,4,2,6,4,5};

int size = sizeof(arr)/sizeof(int);

GoogleSort(arr, size, sizeof(int),  compare);

for(int n = 0; n < size; n++)

cout << arr[n] << " ";

return 0;

}

- ch April 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can i submit code?

- ch April 24, 2016 | 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