Wednesday, November 10, 2021

2021 | A Couple of Coding Interview Questions

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: 

import itertools; # useful in solving combinatorics problems like this one

def nrep(y): # returns True if no digit repeats in y
 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;

def dchk(x,n): # returns True if n is a digit of x
 return str(n) in str(x);

def nstr(a,b): # returns a string of int a followed by int b
 return str(a)+str(b);

def alldigs(N): # returns True if N has all digits from 1 to 9 inclusive
 N=str(N);
 for i in range(1,10): 
  if N.count(str(i))!=1: return False;
 return True;

digs=[2,3,4,5,6,7,8,9]; # can try this with 1 included, will yield same answer

x=itertools.permutations(digs,4); # generate all permutations
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)];

There are 12 answers to this problem. The number pairs that work for x and y are: [(6729, 13458), (6792, 13584), (6927, 13854), (7269, 14538), (7293, 14586), (7329, 14658), (7692, 15384), (7923, 15846), (7932, 15864), (9267, 18534), (9273, 18546), (9327, 18654)]


Question 2: The digits from 0 through 9 inclusive should show up in the product below only once. What are the numbers that satisfy this equation? (a, b, c, d, e, f, g, h) are all digits.

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.

def dig(N,n): 
# returns the nth leftmost digit of N; n=-1 gives rightmost digit
 return int(str(N)[n]);

x=[i for i in range(100,1000) if nrep(i)];
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+=[(i,j,t)]; # z holds answers!

 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: 

  1. 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.
  2. (***) 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.
Please share other interesting puzzles you've come across in the comments. Thanks for reading.