Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Well I guess there are couple of ways we can approach this

1> Design API such that it only Process the functionality and no memory allocation within it.
2> Use the Smart pointer concept for every pointer usage within the API.
3> Can make API's so that for every allocation there must be the deallocation called in. for every condition.

- hprem991 March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 vote

Run your application within a profiler such as `valgrind`. A profiler can provide observations about the amount of memory used, and, more importantly, about possible memory leaks in your program.

- longbelly March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

While valgrind is the right tool to track memory leaks, it is not the solution to this problem, as valgrind does not prevent an API from leaking memory. It merely tells you whether a given program/process does or not, among a plethora of other low level analysis that valgrind supports.

- gagomes December 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Ensure, the memory for the output is provided by the caller.
Api doesn't do any malloc/new. It simply does the processing on input, and copies the output into the output buffer as specified by the caller as a parameter.

- Varun March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1) Determine input data set that achieves 100% code coverage.
If profiling is possible:
2) Call instrumented API with this data set
3) Observe leak reports
If profiling is not possible:
2) Call API repeatedly (i.e. in a loop) with this data set
3) Measure memory usage of process over time
If memory measurements are not possible:
2) Call API repeatedly in a loop for extended periods of time
3) Run other applications on host system to evaluate impact of API

- andreas.schiffler March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

As the author of the API, we ensure no memory leak by
1. making object ownerships clear.
2. protecting heap allocation with smart pointers.
3. output arguments should be provided from caller or use sharedptr.
As a user of the API, we can do some test.
1. use tools with memory leak detection, i.e. valgrind.
2. limit the heap memory of the program and simply try to amplify possible memory leak by repeatedbly calling the API.

- waiging.lau June 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As the scope of API finish,try to unregister all the variable and dynamic memory that is created during API.its like the funtion to perform destructor aftr the funtion call..

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

Write some wrapper function for malloc/free etc dynamic memory allocation API, and add some statistics based on the time-stamp and thread ID in there. Each time before call the API, get a snapshot of the current memory usage of that thread, and after call the API, check its current thread related memory usage is the same of the snapshot taken before the API call, fatal if they are different unless you know that the API itself will need to allocate the memory.

- chenlc626 March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Conceptually the idea is good. But wondering if there is a way to get a snapshot of the current memory usage of a thread from application level without digging into the operating system level issues?

- Romonov March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This doesn't "make sure" the API can't leak memory. The question seems to imply it should never leak by construction. If that's the case, only Varun's answer is applicable: you can't leak memory if you simply don't allocate yourself any in the first place.

- jay March 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the question itself, it is asking people to make sure that an API does not leak memory. If the "people" are API designer, you gonna have a list of strategy for this, as listed above. But all in all, the word is "not allocation memory, or free it after allocation before exiting", it is kind of good wish, but not a systematic method to make sure of this. So finally the people are the API tester, whatever he is unit tester or real user, you need a way to check it in black box style.

- chenlc626 March 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the question arised by Romonov, how to provide a snapshot of a thread application level without digging into the operating system level. Well, there are two cases, one is that the memory leak is caused by the API's call of malloc, another issue is that the memory leak is caused by some functions (beside the direct memory allocation API) called by the API logic, but unfortunately the logic forget the side effect of this API and does not follow some predefined protocol to free it, or what's more, the function the API calls has memory leak, the latter case it is difficult to handle. We may really need to replace some libc implementation of memory allocation, which will call the raw exception based system call, and let the gcc to use the libc when linking the test application.

- chenlc626 March 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Inside the API use a local variable. Increment it when there is allocation and decrement it when some location is freed.This way ensures individual API not creating Mem leak.. But some times freeing can happen in some other APIs. In those cases increment/decrement should not be done. But to track them an ID of the API can be appended to malloc() structure.

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

A good API design should involve the following:
1. No state management. Push all state management to client code.
2. Idempotent. Same input will always provide the same output.
3. No Transaction management. Again, push it to client code.
4. Service oriented - meaning, provide a specific elemental service

When you apply all these design principles, your API code will automatically become lean. From that perspective, everything you will do becomes 'disposable' - all objects and primitive variables that you will use to service a certain request will become disposable.

Then comes the bloody internals. Ensure no loitering, reset your collections references to null etc.

But I believe that a good overall design principle are must for eliminating conditions for memory leaks.

- whitenoise April 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The API should create a mempool and use objects from the mempool. After the API call is completed, you can check for allocated but not freed objects in the mempool.

- Sat December 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Immutable Objects

- Deepesh October 10, 2014 | 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