Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Question needs some clarification.

The input string cannot be modified as the replacement word can be a different size than the word it is replacing. So, implicitly, it needs new string memory.

As you specify, we cannot allocate a local variable and return. So, why not use malloc i.e. dynamic memory allocation and return the pointer?

- Anonymous December 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it possible to return a local array in c ? i dont think so .
Doing a malloc and returning the ptr would work but it would lead to a memory leak .

- kn December 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope my friend....the caller who receives the malloc'ed memory has to free it.

You may want to dig a bit more into usage of malloc. I would recommend a small dig into Unix process layout...if you can find somebody who has worked on Unix internals, it would help to have a discussion on how memory allocation is implemented.

- Anonymous December 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The interviewer insisted that a local array can be returned in C (without returning the ptr to the array) . However , i was not convinced as return of arrays is supported in Java , python etc where the garbage collection is automatically done . But i think it is not possible in C or CPP

- kn December 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

make it static, you can return.

- Sathaiah December 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is possible to return an array in c. Below is the function prototype .

Char [] replace_string( char const *const str )

Inside the function you can declare the array as static and return the array or can dynamically allocate memory for array.

another way you could return the array as a parameter in the function

Static void replace_str(char const *const str, char dst[length] )

- Anonymous December 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1)Having the array as an input parameter where the output can be written into makes it easy but that was not allowed.
2)The prototype you just mentioned does not compile in VC++
char [] replace_string( char * str ).Can you put the entire code / the compiler at least .

- kn December 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The prototype he gave is not pure C/C++ style.

use below instead
char * replace_string( char * str )

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

You can return an array by putting it in a structure. There is no way of returning an array otherwise in C because its always converted to a pointer.

struct ArrayHolder {
char a[100];
}

Now you can just return the instance of the structure you create.

- methe January 17, 2013 | 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