Bloomberg LP Interview Question for Financial Software Developers


Country: United States
Interview Type: In-Person




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

1)There will be no compilation error in any of the implementation.
2) The first implementation there won't be any issues in memory allocation or free.
3)second implementation you can't free(S) because s is pointing to constant memory you can modify s to another pointer but you can't free the current memory allocated.

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

Also worth noting you should get at least a warning in most compilers for implementation 2 for the conversion from a const to non const value.

Decided to verify:
populate.cpp: In function 'void populate(char**)':
populate.cpp:23:11: warning: deprecated conversion from string constant to 'char *' [-Wwrite-strings]

- Kyle April 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

The correct answer is :

error is coming at the point when u are trying to free the memory.

in the 1st implementation, memory is allocated dynamically using malloc and so u can free that using free().

where as in the 2nd case memory is allocated on the stack. and u cannot use free() to deallocate the memory on stack.

free() should only be called on memory you allocated (or on NULL).

- ChillGuy February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In 2nd case memory is not allocated on the stack. It is allocated on space (generally in the read-only static data section) for the string literals

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

You should also get a warning for the 2nd implementation that you're doing a conversion from a const to a non const value.

- Kyle April 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 6 vote

First implementation is correct. There is nothing wrong in it.

But for the second one compilation error as "Prasad" returns a const char* but *str is just char*.

- Satya February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't give any compilation error. But it throws an runtime error while trying to execute this statement.
free(s)

Free expects a void *

- Me February 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The correct answer is :

error is coming at the point when u are trying to free the memory.

in the 1st implementation, memory is allocated dynamically using malloc and so u can free that using free().

where as in the 2nd case memory is allocated on the stack. and u cannot use free() to deallocate the memory on stack.



free() should only be called on memory you allocated (or on NULL).

- ChillGuy February 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here u can't free it as str is previously allocated memory
in stack.only heap memory can be freed;

- mani 4m sklm March 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// In this it prints first string and now free the memory here it is given that how to free memory



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

void populate(char **s);

int main() {
char *s;
populate(&s);
printf("%s\n", s); // should print "Prasad"
s = NULL;
free(s);
printf("%s\n", s);

return 0;
}

void populate(char **str) {
// 1. The next two lines is one implementation
*str = (char *)malloc(sizeof(char) * 7);
strcpy(*str, "Prasad");

// 2. This line seperately is another implementation
*str = "Prasad";
}

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

Terrible style for C++. Better would be

void populate(char*& str)

The problem is with

*str = "Prasad";

"Prasad" refers to memory in the initialised part of the .data segment. free() should generate an error (but doesn't have to - behaviour is undefined) and the compiler should complain of a assignment of a const char [] to a char*.

- sokane@bowmain.com August 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm surprised no one mentioned that there is a problem with the first approach because the allocated memory is never initialized. So, when we print it, the string will not be prasad as the string was not null terminated.

Second approach has couple of issues as already discussed.

- Karthik R October 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Even if we const cast and assign the const string to *str, trying to free it will probably crash the program since string literals are stored in read only area of the memory with static lifetimes.

- sadhan February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

nothing wrong in the implementations. the difference i see is that contents of a string created in the first implementation can be modified. whereas contents of a string in the second implementation can not be changed, since it is a string literal.

- nvseenu February 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

*str is a pointer, a variable , why it cannot be changed to point to another const string ?
I changed "Prasad" to "ppp",but it said:
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x08048587 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x721a2)[0x9321a2]
/lib/i386-linux-gnu/libc.so.6(+0x723e8)[0x9323e8]
./a.out[0x8048461]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x8d9113]
./a.out[0x80483a1]
======= Memory map: ========
001ff000-00200000 r-xp 00000000 00:00 0 [vdso]
0027d000-00299000 r-xp 00000000 08:01 6423482 /lib/i386-linux-gnu/libgcc_s.so.1
00299000-0029a000 r--p 0001b000 08:01 6423482 /lib/i386-linux-gnu/libgcc_s.so.1
0029a000-0029b000 rw-p 0001c000 08:01 6423482 /lib/i386-linux-gnu/libgcc_s.so.1
0044d000-0046b000 r-xp 00000000 08:01 6430185 /lib/i386-linux-gnu/ld-2.13.so
0046b000-0046c000 r--p 0001d000 08:01 6430185 /lib/i386-linux-gnu/ld-2.13.so
0046c000-0046d000 rw-p 0001e000 08:01 6430185 /lib/i386-linux-gnu/ld-2.13.so
008c0000-00a3c000 r-xp 00000000 08:01 6430166 /lib/i386-linux-gnu/libc-2.13.so
00a3c000-00a3e000 r--p 0017c000 08:01 6430166 /lib/i386-linux-gnu/libc-2.13.so
00a3e000-00a3f000 rw-p 0017e000 08:01 6430166 /lib/i386-linux-gnu/libc-2.13.so
00a3f000-00a42000 rw-p 00000000 00:00 0
08048000-08049000 r-xp 00000000 08:01 6030435 /home/xieying/interview/a.out
08049000-0804a000 r--p 00000000 08:01 6030435 /home/xieying/interview/a.out
0804a000-0804b000 rw-p 00001000 08:01 6030435 /home/xieying/interview/a.out
0a006000-0a027000 rw-p 00000000 00:00 0 [heap]
b777a000-b777b000 rw-p 00000000 00:00 0
b778a000-b778d000 rw-p 00000000 00:00 0
bfab2000-bfad3000 rw-p 00000000 00:00 0 [stack]
已放弃

- Anonymous February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

居然有中文

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

没啥稀奇的

- Anonymous May 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 4 vote

There will be no compilation error in any of the implementation.
First implementation copies the data to the allocated memory. The second implementation causes a memory leak as str is allocated some memory but is over-written by some other address. There is no way to free the memory allocated by malloc.

- Ravi February 10, 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