When typing numbers into a calculator, some numbers, when the calculator is flipped upside down, can be read as a word.
"7734" -> "HELL"
"002" -> "ZOO"
(bonus example: "5318008")
Suppose you are also given a mapping of characters to their respective letters that they can be read as when the calculator is upside down:
1 = I, 2 = Z, 3 = E, 4 = H, 5 = S, 7 = L, 8 = B, 9 = G
(6 has no corresponding letter)
Q: Write a function that takes in a number as the input (that would be typed into the calculator), and a list of valid words, and returns whether the number, when flipped upside, contains a valid word.
- Clarify with the interviewer whether the input should be a
string or a
int, and discuss how it would be better to have the input be a
int would remove leading zeroes, making inputs like
"002" just become
2. Also, we are only typing numbers into the calculator and not performing operations, so when you type 002 on a calculator it has not been converted to 2 yet.
- I think this problem is more about how you communicate your thoughts than to pose a difficult coding challenge.
- My solution was O(n) but was asked "are there any optimizations you can think of?" and couldn't think of any. My solution was to parse the input in reverse, convert it to a word based on the given mapping, and then check if it is present in the given list of valid words.