Work in HR or Recruiting?
Google
www.google.com Mountain View, CA 5000+ Employees
Work in HR? Complete Your Profile

111 interview experiences Back to all Google Interview Questions & Reviews

Interview Question for Software Engineer at Google:
Aug 15, 2012

What is the output? int n = 1; puts(((char*)&n)[0]==1?"Y":"N");


Tags: technical
Add Tags [?]

See more for this Google Software Engineer Interview

Helpful Question?  
Yes | No
Inappropriate?

Answers & Comments (6)

7 of 7 people found this helpful

Aug 15, 2012

by Interview Candidate:

It can be Y or N and the answer depends on the "Endianness"
Helpful Answer?  
Yes | No
Inappropriate?

4 of 4 people found this helpful

Aug 18, 2012

by rkt:

yes..depends on the system architecture whether it is big endian or little endian
int n = 1; stores it in the following way in 4 bytes
00000000 00000000 00000000 00000001 (increasing memory addresses)
(char*)&n will make n de-reference to only 1 byte instead of 4 bytes.
For big endian, (char*)&n[0] will be 00000000 and for little endian it will be 00000001
Helpful Answer?  
Yes | No
Inappropriate?

0 of 6 people found this helpful

Aug 22, 2012

by zboot:

Umm, so &n returns the address of n, we then cast it as a pointer to char, we then dereference the first element at that pointer [0] and read it as a char. the char corresponding to a value of 1 is not the same as the actual value 1. So, the comparison will return false, and we'll print N.
Helpful Answer?  
Yes | No
Inappropriate?

1 of 1 people found this helpful

Sep 5, 2012

by ????? ?????????????:

uh...

 int heads = 13;
  for (int bullets = 0; bullets < heads; bullets++ ) {
    int n = bullets;
    puts(((char*)&n)[0]==n?"Y":"N");
  }

gives:
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Helpful Answer?  
Yes | No
Inappropriate?

Sep 5, 2012

by iosif vassirionovich:

int heads = 130;
  for (int bullets = 0; bullets < heads; bullets++ ) {
    int n = bullets;
    puts(((char*)&n)[0]==n?"Y":"N");
  }

gives:
Y
Y
....
....
N // 128 bullets
N // 129 bullets

(overflows the signed char)
Helpful Answer?  
Yes | No
Inappropriate?

Sep 7, 2012

by Herakleides:

Not even close guys. The code as written is not portable and invokes implementation-defined behavior. The problems involve whether or not a pointer to char corresponds to a pointer to unsigned char or pointer to signed char. char can be either and it is left up to the implementation. It is important because not all bits are value bits. An implementation can encode a signed char with value bits + pad bits + sign bit and conversion between pointer to int to pointer to char is not guaranteed to produce a meaningful value of the destination type. unsigned char has value bits while something like an unsigned int can have both value bits and padded bits. All that means, is that you can't go about mucking around with bit patterns and converting them between pointer types where the pointer type is not an allowable type to be converted to as outlined by the ANSI/ISO/INCITS standard that defines the abstract machine for the C programming language (9899:1990) and (9899:1999).

A lot of this is also very similar to the C++ language so it also holds.
Helpful Answer?  
Yes | No
Inappropriate?

To comment on this question, Sign In with Facebook or Sign Up

Tags are like keywords that help categorize interview questions that have something in common.