Amazon Interview Question for Software Engineer / Developers






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

Check for the NULL after every malloc and free the memory at the end of the program (though its not required in this case but its generally a good practice to do that).

- mja August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

50 bytes are lost ,there will be a memory leak.So use free(p) and free(q)

or calculate size of string and then pass the size to malloc.

- gaurav March 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The problem with the code is that a pointer is having the address of the memory location having the string.so change it it is not authorized ..which we try to do in concatenation.strcat().

- hurricanedurza January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think there is no error

- kamal August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think we will get the desired output but we should free the memory.

- gaurav October 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think there is no error

- kamal August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we should free the memory.otherwise memory leak will occur.

- gaurav.2897 October 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

not judge whether malloc succeeds.

- netbsd8 August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am concerned here about the operation strcat.
P is a character pointer, and using malloc it got a space for 25 characters.
Similar is the case with Q.
Both have some values copied in their memory locations.
So, for P, value will be like amazon_ _ _ _ _ _ _25thlocation
For Q, value will be like hyd_ _ _ _ _ _25thlocation.
When we concat them, will the empty spaces in P and Q be trimmed off or not?

- PQ August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

After strcat(), the next character to amazonhyd will be'\0'. So there should not be any issue and we will print amazonhyd as output.
Checking the malloc's return address and freeing the memory seems to be the missing part here.

- ardent.1981 August 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

all is fine but one can say a missing free mmory part but i think that should be not a big issue here

- geeks August 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ya u r rt geeks

- abhi September 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i too agree with geeks and ardent...

- beginner August 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As such, nothing is wrong with the program, unless you want to implement coding best-practices:

1. You should check return of malloc - whether it has returned NULL or not.
2. Use strncpy, and strncat; Although this is not be an absolute requirement as long as malloc size and strings are hard-coded.
3. main should return int, and should also return an exit value.
4. p and q should be initialized to NULL.

- Nihar August 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think main didn't return int because it is C not C++

- airfang613 August 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i compiled
no problem
it prints amazonhyd

- aditya August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

great! hopefully you'll stay unemployed!

- nasty August 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

lol

- ACP Pradyuman September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Nasty, I'm sure you're still unemployed!

- Onsechrotus October 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Exactly.
A char usually takes up 2 bytes as far as I remember.
So allocating 25 bytes may be enough for "Amazon"
But say we wanted "Hyderabad" to be concatenated to "Amazon", resultant string won't have enough space.
So better always allocate in terms of sizeof( )

- Harsha January 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

dont we have to mention the (size of char) fo rmalloc? Usually if you have malloc(i) and i is integer it works.. here it is char.. so the value is 1 byte and therefore working properly..

- r August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think no problem here it works fine

- laasya1991 September 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think after strcat it should be assigned to a varible and then print

- lakshmi prasanna September 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Printf uses character pointer and prints it until it finds a null character. As malloc returns a memory with garbage data printf may print some garbage data after two strings 'amazon' and 'hyd'.so we need to either initialise the memory allocated with malloc to null or allocate the memory using calloc.

- Satish November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude check my answer
the 1 ur givin is not valid

- hurricanedurza January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

there is no error. An error would have occurred, if no memory is allocated to the char*. As the compiler doesn't know the termination length, it cannot concatenate or copy (strcat n strcpy do not work ) and throws a segmentation fault.

- Newbie February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no error

- sripriya March 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no error

- shashi January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The issue seems to be the fact that strcpy() and strcat() do not check how much memory is actually allocated to the addresses (similar to C doing no bound checking on array). For example, even if one does this, it still appears to work:

char *p = (char *) malloc (0);
char *q = (char *) malloc (0);

- Sina November 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

maybe you didn't free the memory!!

- main() August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think in printf statement there is required another %s for q

- suraj badhan August 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

malloc does not return any address after allocation.
Thus assignment of malloc to a char pointer is wrong

- gsn August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't comment just for the shake of commenting..

- Anonymous August 07, 2011 | Flag


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