Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Gildas Montcho
Isn't there an error in the description of the algorithm?

Gildas Montcho

Oct 5, 2024

In the algorithm walkthrough, at step 6:

  1. At this point, the stack contains the binary representation of the number, but in reverse order. This is because the first bit we calculated (the least significant bit, or the "rightmost" bit) is on the top of the stack, while the last bit we calculated (the most significant bit, or the "leftmost" bit) is on the bottom of the stack.

From what I understand, the first bit (the "rightmost" bit) calculated is at the bottom of the stack, and the last bit (the "leftmost" bit) is at the top of the stack.

Eg: If the number is 4:

4 % 2 = 0 => 1st bit (directly pushed onto the empty stack, so it is at the bottom of the stack, first in last out)

0

0

Comments
Comments

On this page