Interview Question


Country: United States




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

modulo has a distributive property
(a * b) % n is equivalent to (a % n * b % n) % n

pow(a,b) is basically multiply a with each other b number of times. so instead of calculating pow(a,b) first which will be a huge number since b is larger than long range, we first calculate a % n and then that value we multiple b numbers of times.

Every time a%n times b goes above long range, we take the % again ... here is a simplified example for clarity

a = 5 b = 4 and n = 3
pow(5,4) % 3

which is the same as
= (5 * 5 * 5 * 5) % 3
= 625 % 3
= 1

assuming pow(5,4) goes above the range, we can also write pow(5,4) % 3 using 5%3 approach like below
= (5 * 5 * 5 * 5) % 3
since 5%3 = 2, equation becomes

= (2 * 2 * 2 * 2) % 3
= (4 * 4) mod 3 --> here assuming 4 * 4 goes above long range, we take mod again
= (4 % 3 * 4 % 3) % 3
= (1 * 1) % 3
= 1

- Saurabh July 11, 2018 | 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