Bloomberg LP Interview Question






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

int main(){
	int *myArray;
	myArray = new int[2]{ 100, 200 };
	int& ref = myArray[0];
	cout<<ref<<endl;
	delete[] myArray;
	cout<<ref;  \\dangling reference
}

- SamOnYoun November 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

It`s not C++ code: dynamically allocated arrays cannot been initialized. And in commens wrong slashes. More simple code to show problem:

int *p = new int(11);
int& ref = *p;
cout << ref; // output 11
delete p; // <--- here p is being deleted and reference ref is no more alive
cout << ref; // output garbage

- Anonymous November 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Well, the comments are wrong. but it is C++ code. you can initialize an array as "myArray = new int[2]{ 100, 200 };"
it is called initialization list. compile your code with -std=c++0x switch in Makefile.

- SamOnYoun November 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@THE ONLY EXISTING Anonymous to date in the second line of the program
int & ref = *p;
*p would try to stoe the value pointed by the pointer dont u think into reference ref which lead to compile error , correct me if am wrong

- Deric January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;

int main()
{
int& sum(int a, int b);
int &a = sum(5,6);
cout << a << endl;
return 0;
}
int& sum(int a, int b)
{
int c = a + b;
return c;
}

- PK August 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"int &a" in main() is dangling reference.

- PK August 21, 2013 | 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