Interview Question for Software Engineer / Developers


Country: United States




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

When an array is declared in the stack or heap, those 2 statements are equivalent since the compiler translates p[2] in *(p + 2 * sizeof(char)) = *(p + 2 * 1) = *(p+2) since 2 and 1 are constants.

String literals are usually stored in read-only segments of memory. So the compiler might know the value of p during compile time. This still does not make a difference in efficiency between the 2.

- Miguel Oliveira September 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. Compilers have become much smarter as compared to when this interview question was first created. Clueless interviewers still continue to use that though...

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

Suppose p is allocated dynamically, e.g., char *p=(char *)malloc(size), so compiler won't know the address of p until runtime.

You may find the answer with disassebmly code..

- James Liu September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The compiler doesn't know the address of p, but that is true for both cases. It does not make a difference.

- Miguel Oliveira September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

They are same, at least for the g++ I used.

Here is a sample c++ code test.cc:

int main()
{
const char *p = "hello";

L1: char c = p[2];
L2: char b = *(p+2);

L3: {}
}
~
g++ -S test.cc, and you get:

.file "test.cc"
.section .rodata
.LC0:
.string "hello"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movq $.LC0, -16(%rbp)
.L2:
movq -16(%rbp), %rax
addq $2, %rax
movzbl (%rax), %eax
movb %al, -2(%rbp)
.L3:
movq -16(%rbp), %rax
addq $2, %rax
movzbl (%rax), %eax
movb %al, -1(%rbp)
.L4:
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits

you can see the generated code for p[2] and *(p+2) are same.

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

USELESS question. Why are they asking this?

They are absolutely equivalent in every way. An early phase of a compiler will convert a[i] to *(a+i).

It gets mapped to same executable code.

In fact, most compilers would even accept 2[p] because it gets converted to *(2+p) which is *(p+2). Unless the compiler maker put useless extra checks to exclude this.

BTW, this is a useless interview question.

- bigphatkdawg September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

THEY ARE THE SAME. 100% sure of it.

- bigphatkdawg September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

*(p+2) is more efficient...

lets the start address is 1000, *(p+2) will add 2 to base address and fetch the data at address location 1002... this one fetch the data in single step

where as p[2] move one by one location from base / start address to the desired address 1002, this one fetch the data in n steps (for p[n])

- algos September 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"where as p[2] move one by one" why do you say this? i've never seen such argument before. that's not how it works in arrays

- Miguel Oliveira September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It doesn't work this way. p[2] is syntactic sugar for *(p+2).

Also, if p refers to address 1000, p + 2 is not 1002. p + 2 = 1000 + 2*sizeof(*p) = 1000 + 2 * sizeof (int) = 1008 for 4-byte ints.

- eugene.yarovoi September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

they are equivalent, but p[2] is more efficient as here a de-referencing is avoided.
Let me know, if u hav different thoughts on this.

- Varun September 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