Cover image for Wargaming
Logo

Wargaming

Engaged Employer

Wargaming

Add an Interview

Interview Question

Software Engineer Interview

-

Wargaming

Divide a number by 7 without using division/modulo operators.

Interview Answers

3 Answers

1

You have to understand how bit shifting works, and what special property shifting to the left or to the right has. Your answer will be a representation of a bit shift + offset. Additional trivia: What happens when you overflow the min/max range?

Anonymous on

1

int n = ...; int res = 0; while (n > 8) { int j = n & 0x7; n = n >> 3; res += n; n = j + n * 0x1; } if (n >= 7) ++res;

Matthew on

0

static int binary (int num){ long lo = 1 , hi = num ; while (lo > 1 ; if (mid * 7 >= num) { hi = mid ; } else { lo = mid + 1 ; } } if (lo * 7 > num) return (int)(lo - 1); return (int)lo ; }

Binary Search on

Add Answers or Comments

To comment on this, Sign In or Sign Up.