Google Interview Question for Software Engineer / Developers






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

The Problem in the give code [original code] is that both a[] and b[] are global variables and so there is no order of there construction. a[] can get constructed first as is happening with siva.sai in gcc compiler. But he is lucky. If any compiler writes the code to create global objects in such a way that b[] is created before a[] then "boom"!.

We should not write such a code assuming order of creation of global variables to be in certain way. Standard does not define any such order. For local variables, yes you can guess the order of creation of objects [but these days smart optimizers in compiler coded tweek with them too!]

- Sourav Sain October 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

array bound is not an integer constant
main doesn't have a return value

- SMK September 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

actually there is 1 problem. length of array b should be known at compile time and variables are allocated at run time.

As far as return type of main is concerned. Its the only function in c++ which implicitly gets return type.

- Patron September 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

...
const int a=3;
int b[a];
...

there should be a similar problem in the code snippet above according to your reasoning, but it works.

- ankit September 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Note: with c++11 this code:

using namespace std;
constexpr int a[]={1,2,3,4,5};
int b[a[2]];

Becomes perfectly valid.

- Thomas June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thomas is right.
The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time.
but
const int a[]={1,2,3,4,5};
int b[a[2]];
is error: expression must have a constant value

- Sirius April 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

looks like, the question is given to misled you to think that, b[a[2]] does not work, but it does work perfectly. b[a[2]] will be evaluated as b[3] and so, b will be created as an array of 3 elements. The only visible problem with the code is that main does not return any int value.

- surendra September 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Patron is right here..
Array length should be known at the compile time...

- DashDash September 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It *is* known at compile time, because the array "a" is known at compile time thanks to the const keyword.

- Bullocks November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The definition of b would work if it was inside a function.
Outside a function, you will get a compile time error.

- Anonymous September 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It will give compile time error in statement:
int b[a[2]];
As value of a[2] is not known at the compile time.

- Rajiv September 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for static allocations like the one "int b[n];" n should be a constant positive integer or a const int variable(positive) defined before "int b[n];" statement.

- ankit September 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a is constant but a[2] is not a constant !!

- ankit September 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I run following program but I have not seen any error in LINUX gcc compiler.

#include <iostream>
using namespace std;
int main()
{
        int a[] = {1,2,3,4,5};
        int b[a[2]];
        return 0;
}

Folowing code thrown an error "Error: variable-sized object ‘b’ may not be initialized "

#include <iostream>
using namespace std;
int main()
{
        int a[] = {1,2,3,4,5};
        int b[a[2]] = {1};
        return 0;
}

any one please explain , why I am getting error in second program and no tin first program ?

- siva.sai.2020 October 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi ,
a and b are global variables. Their sizes are allocated whenever they are declared whereas in the case of local variables the size are only allocated when they are defined.

So in this case the compiler is unable to decide what size to allocate.

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

nice

- siva.sai.2020 May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Deep jumping in references are not allowed at compile time. In case of

const int a;
int b[a];

one direct reference to the variable. But in case of

const int a[] = {0,1,2};
int b[a[1]];

first 1 store in temporary variable then ->used in a[1] ->first reference
second b[a[1]] ->second reference (depth)

- pankaj November 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

in C++, const can be used with complex data types like arrays , structures, but it does not mean that you can access the value at compile time. It means the storage is constant and cannot be changed , but retrieving what is present at that storage at compile time is not allowed.Its allowed for simple types like int, char, float.

- dev December 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Verified on Windows, compiler throws error that 'expected constant expression'

int main()
{
int a[] = {1,2,3,4,5};
int b[a[2]] = {1};
return 0;
}

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

using namespace

violates Google's C++ style.

- John December 10, 2012 | 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