Google Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

Hey guys, check this out. Might not work on some platforms though.

#include <cstdio>
#include <cstring>

int main() {
    const void* res = memchr("Syshsh Pavlik", 'p', 16);
    // printf("%p\n", res);  // uncomment this line and see what happens
    return !*(int*)res;
}

ideone.com/jtkgNj

- PPY January 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Brilliant!

This doesn't require multithreading or lazy loading (plainly prints a variable). Althought the example is a bit contrived, it shows what may happen: the string you are passing to printf lands somewhere in the constant data section in the executable (like .rodata) and may influence the memory layout (for example, make the '\0' character nearer for a non-terminated string etc.). In this example, the 'p' found by memchr when the printf is uncommented is from "%p" passed to printf!

Such bugs also suffer from "works on my machine" reproduction hardness, because the strings in the read-only section may be reordered and realigned at the compiler's will (and they are most certainly not preserving the order they occur in the source file due to constant string duplicate elimination done by most compilers).

I wish I could +1 it, but some bug in careercup.com just redirects me to my Google account, and when I enter the password nothing happens.

- xplainr January 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

its a clear case of lazy loading

- Anonymous December 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Guess this has to do with memory management at OS level. To make the point clear, we need to understand that once the program reaches machine level the order in which its instructions are to be executed can be messed up. I.e. instruction 3 will be executed first and then instruction 2 and then 4 and so on. However it will be made sure that the end result remains the same.

In our case, the compiler and runtime environment are allocating contiguous memory blocks to each line in function. It ends up creating a substantial memory block in which the function is supposed to operate. However for some reason (say an OS bug or something) the execution falls outside of the allocated block. A printf statement expands the block and runtime error is covered.

Having said that it's important to note that it's possible to avert the error by inserting an assignment statement instead of a printf one. It's just that printf takes more memory allocation and will always cover the worst case scenario.

- ompranav January 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think it maybe related to the late evaluation. Some variable don't get the evaluation value until it is being used. For example, the django's database query result didn't get the whole value until the variable being accessed.

- ManicMovier December 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Multi threading issues? Printf is consuming some time which resolves a multithreading issue?

- naren December 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

She didn't mention anything about multi-treading. She asked about different possibilities.

- maya December 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok I meant it as an answer and open for discussion on this. Concurrency or multithreading issues is my best bet on this..

- naren December 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is an open ended question. There will be more clarity on what is the error information in the runtime error. Could be typecasting, lazy loading or multithreading issue until there is further clarity.

- Victor December 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, I get buffer overflow and multithreading issues, but I am confused about typecasting and lazy loading, at least under the rules of java. I guess if this is C++, virtual pointer offsets might change during typecasting, but I believe they are not supposed to. As for lazy loading, I could see this happening in a dynamic language like groovy in which variables are secretly replaced by getter and setter methods behind the scenes, but this feels like cheating to me.

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

In addition to the other answers, I've seen something very similar when some part of my code has had undefined bejaviour in it, such as reading a vector value from outside of its range.

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

As Victor said - this is open-ended question. I can give at least 5 explanations why this can happen.
However, most likely that happens because of the uninitialized variable on one of the future stack frames which is created after printf call.
Without printf unitialized variable has some value (whatever happens to be on the stack). call to the printf modifies the stack changing the future value of that variable. This triggers different runtime behavior.

- 0xF4 December 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In java, we could do something like this :

public class Solution {

public static int data()
{
Integer a=null;
//System.out.println((a=0));
return a;
}

public static void main(String args[])
{
data();
}
}

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

public class Solution {
	
	public static int data()
	{
		Integer a=null;
		//System.out.println((a=0));
		return a;
	}
	
	public static void main(String args[])
	{
		data();
	}
}

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

Could also be the simple case of your code not following the same path due to some Math.random() calculation returning a different result. For instance,

if(Math.random() > 0.5) { return null } else { return true }

So when you added the print statement, it happened to be a different random value and you didn't get the error. So maybe it didn't have anything to do with your action of adding the print statement, event though you think it is related.

- Random variable June 06, 2017 | 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