Hacker News new | ask | show | jobs
by guccihat 515 days ago
I think the interviewers were looking for the key insight that a number is divisible by 3 iff the sum of the individual digits is divisible by 3. That is easy to verify, even constrained to using single digit data types. To be clear, I am not saying this was a great interview question and I agree the solution OP came up with is impressive.
3 comments

> a number is divisible by 3 iff the sum of the individual digits is divisible by 3

And how do easily verify this divisibility when "Numeric types, number literals and their associated methods and operations are forbidden?"

Since I read the constraint to allow for single digit numeric values*, I guess they are looking for a solution similar to:

  function isDivisibleByThree(num: string): boolean {
      let mod3 = "012012012012";
      let modulo = "0";
      for (const digit of num) {
          modulo = mod3[Number(digit) + Number(modulo)];
      }
      return modulo === "0";
  }
If adding two single digit numbers is also prohibited it can be implemented with a lookup and keep everything in string representation.

"The programmer can use whatever representation they see fit with the only restriction being that it could only contain letters, numbers and symbols that could be typed with a single stroke"

> If adding two single digit numbers is also prohibited it can be implemented with a lookup and keep everything in string representation.

Yeah uh I guess you missed the SUM_TABLE part of the article? That's what they're doing. And that's why the rule against matrices was added.

That version of the code is 80% checking if "the sum of the individual digits is divisible by 3", 20% the rest of the fizzbuzz.

The constraint only states not to use hard coded matrices as I read it. Here is a version with number addition removed, this works fully on strings.

  function isDivisibleByThree(num) {
    let modulo, mod3 = "012012012012";
    for (const digit of num) {
        [modulo] = mod3.slice(modulo).slice(digit);
    }
    return modulo === "0";
  }
Using slice twice is pretty clever. That might work.

But my real point is they did have the digit sum insight. Their code was already doing your previous suggestion, and if there's a compact way to slice in typescript types it could be adapted to this new method by replacing SUM_TABLE and changing one other line.

The only difference is that they're doing a sum modulo 9 instead of modulo 3, but both of those work fine.

I see what you mean now, you are right. My only intention was to demonstrate that a TS solution is possible - one that does not rely on the type system but one that still observes all the constraints listed. I think this was questioned by some in the thread (but not you).
I would have failed this question badly. I'm curious, how would you check that without access to any number or math operations? I think I would try something similar to what he did with his sum_table, but that too was disallowed. They're constraints I've never had to deal with, nor seen before.
Math operators like division aren’t native to your (traditional) cpu, they are implemented in code, for example using similar algorithms as the ones taught to us in grade school to multiply big numbers one digit at a time on paper and “long division”.

All math operations can be implemented with bitwise operators, too, i am pretty sure

Likely the interviewer specifically needed the candidate to do that, implement the math, and tried to steer them that way numerous times (no sum table, dont use the type system, no math operators). Thats likely also why they suggest allowing limited use of Google, because they realize many people will need a refresher on bitwise operations. But they don’t want to outright tell you what to search for, they needed to see some resourcefulness. When they suggested OP was cheating they likely didn’t mean it personally and actually wanted to help steer OP towards an acceptable solution. Rather than saying it’s cheating they could have said it avoids the main thing we need to see, or outright say “please implement the low level math from first principles”

In my opinion the candidate showed resourcefulness in their own way indeed, but sometimes its not even up to the person administering an interview for example if they have been given a rubric.

Bitwise operators work on numbers. That's against the rules.

And while division can be implemented as repeated subtraction, you are not going to find any CPUs 4 bits and up that don't have an adder. It would be ridiculous to try to handle addition/subtraction in software.

> Math operators like division aren’t native to your (traditional) cpu, they are implemented in code

If you are talking about tiny microprocessors or old ARM chips, sure. But so are programming languages! They should have really asked him to code his solution in machine code then. After all, that's what you typically do in a NodeJS job :)

You clearly did not read the article...