Of course if they struggled with that one, there was likely no way the would get the question where we asked them to write a function that takes an integer and return an integer that is the result of each of it's digits being squared.
Of course if they struggled with that one, there was likely no way the would get the question where we asked them to write a function that takes an integer and return an integer that is the result of each of it's digits being squared.
Honestly this one is really to help us look at a person's thought process because there are multiple approaches to solve it. Usually people either go some variation of math route or a string manipulation route.
Examples of Programming Riddles I have seen first hand: How to invert a binary tree. Implement a binary search algorithm. Design an app that generates all possible phone numbers that a knight chess piece could create by moving across the dial-pad.
Those are riddles. This is not.
In real work there is a whiteboard, but everyone in the room is part of the discussion. You're set in the context of the work being done. You are aware of the end goals. You look up things on the internet. You experiment and write tiny programs at a repl. You think about the work being done both actively and while sleeping subconsciously maybe even during your commute. You have conversations with coworkers - stress free. When people say "it took me only 15 minutes to solve this at work" they forget the prior 2 years of context setting.
Your question is on par with a math problem. If they don't immediately think about string manipulation. I would rather see a candidate walk through some of their code on Github as better example of their skillset or having a candidate do a PR review over existing code shows much more insight. Reading code is harder than writing code, especially without context. Both activities highlight communication.
One of the best interviews I ever had was doing pair programming on a real computer. The interviewer was driving (removing typing gitters, editor issues) TDD style. Write a test. How do we get that test to pass? Is there a chance to refactor? Everything that appeared on that screen had to be communicated. That was by far the most fun I've ever had in an interview.
We've done some of the same pairing type activities during our interviews. And I could tell within 2 minutes if the person was used to pairing. When a negative review would come back a lot of the time it was because "their communication could be better". Vibe is a thing and pairing is a skill.
At the end of the day interviewing is about playing games and the employer makes the rules.
[(d%10**(n+1) - d%10**n) / 10**n for n in reversed(xrange(int(log10(d)+1)))]
will be a list containing the base 10 digits of d as integers.Technically, you need to do
from math import log10
to make this work, but you can fit that on the same line with a little hacking around using __import__, such as this quick and very dirty attempt: [ [(d%10**(n+1) - d%10**n) / 10**n for n in reversed(xrange(int(log10(d)+1)))] for log10 in [__import__("math", fromlist=["log10"]).log10]][0]
Edit:Just to carry this a little further, if we interpret "a new integer that's the result of each of the original integer's digits squared" as doing something like this:
129 -> 1481
(i.e. 9 gets replaced with 81), then we can do it in one line like this: int(''.join([str(x**2) for x in [(d%10**(n+1) - d%10**n) / 10**n for n in reversed(xrange(int(log10(d)+1)))]]))For example, take 999 (N). Using only math, how can I determine the value of each digit?
(be sure to take abs(N) and record value of N/abs(N) for later)
res = N - (N - 1). Digit = res + 1; If Digit == 10, Digit = 0.
So, given that function to find each digit, you can build a loop using 10^i to detect each digit (subtracting the sum of all preceding digits and dividing by 10^i to trim the lower order digits you've already resolved).
We'll need a place to store each digit, such as an array.
Of course, this assumes integers in base 10. If they are another base (8, 16), replace the 10^i above with the base.
It's also ambiguous as to if the function should just return a concatenated version of the results or some possibly a sum.
In any case, using a cast to string would be way easier.
I've also seen incrementing an exponent. And I hear from more math oriented people that there are other ways.