In this post we explore a couple more simple coding interview questions. These were recently asked of 4th grade math students to be solved with paper and pencil, so should be fair game for job interviews. My approach below is not the most optimal for any of the questions, but the solution is easily coded in 3-5 min. You should of course try to improve on these. And solving these analytically with paper and pencil is "left as an exercise to the reader".
Question 1: using the digits from 1 through 9 only once each, find two numbers such that the second is twice the first.
My approach here starts with common sense that I then codify into the program. Given the digits 1 to 9 only, we can split this into a 4 digit number (say x) and a 5 digit number (y say), with the 5 digit number being double the 4 digit number. Before reading further, convince yourself that the leftmost digit of y has to be a 1 for the math to work. The code below works regardless of whether you figure this out or not, but setting the parameters with this understanding simplifies the calculations and makes your code more efficient. Here it is:
t=str(y);
for i in t:
if t.count(i)>1: return False;
return True;
def xdig(x,y): # returns True if there is no digit overlap between x and y
return len(list(set(str(x)).intersection(set(str(y)))))==0;
return str(n) in str(x);
return str(a)+str(b);
N=str(N);
for i in range(1,10):
if N.count(str(i))!=1: return False;
return True;
r=[]; # list will hold answers when done
for i in x:
n=i[0]*1000+i[1]*100+i[2]*10+i[3]; # construct 4 digit number from digit permutation
y=2*n; # y is 2 times x
if nrep(y) and xdig(n,y) and not dchk(y,0) and alldigs(nstr(n,y)): r+=[(n,y)];
abc x de = fg0h8
To solve this problem, we reuse some of the functions already defined above, but add a couple more to help with our work. To solve this problem, we call the number with digits abc as x and that with digits de as y. Code and answers follow below.
# returns the nth leftmost digit of N; n=-1 gives rightmost digit
return int(str(N)[n]);
y=[i for i in range(10,100) if nrep(i)];
z=[]; # collect answers here
for i in x:
for j in y:
t=i*j;
if len(str(t))==5 and dig(t,2)==0 and dig(t,-1)==8 and xdig(t,i) and xdig(t,j) and xdig(i,j) and nrep(t):
Z is: [(297, 54, 16038), (594, 27, 16038)] - there are 2 possible solutions.
Photo by Miguel Á. Padriñán from Pexels |
Why is it important and useful to learn to solve these kinds of problems in python?
With the pandemic, interviewing and hiring looks more at a global talent pool. I regularly interview candidates remotely. How then can I ask them to code something in a coding test or otherwise, and get a quick sense of whether they are able to code correctly let's say even if I am not able to see their code as they are coding it, but just at the end? Well, problems like these where there is a definite correct answer that can be verified, and where the answer cannot be easily googled (or "binged" if that is also a verb), are useful in such situations. I use these kinds of questions regularly these days.
Another couple of interesting questions, left to the reader:
- True or False: the 30th leftmost digit of the 3000th Fibonacci number is prime. [The Fibonacci series starts 0, 1, 1, 2, 3, ... and each successive number is the sum of the previous two series elements.] This one is trivial for python enthusiasts.
- (***) What positive number doubles when its last digit becomes its first digit? Very challenging problem! See how Presh Talwalker solves it here. For those that don't know, his "Mind Your Decisions" YouTube channel has some excellent math puzzles with solutions that help sharpen your thinking.
No comments:
Post a Comment