Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
2,368 views
in Interview-Experiences by Expert (34,270 points) | 2,368 views

2 Answers

0 like 0 dislike

solution

 

My solution in Python:

 

mapping = {"0": "O", "1": "I", "2": "Z", "3": "E", "4": "H", "5": "S", "6": "G", "7": "L", "8": "B", "9": "G"}
valid_words = {"ZOO", "HELL"}

def is_valid_word(num, mapping, valid_words) -> bool:
    # convert num to corresponding word
    word = ''
    for i in range(len(num) - 1, -1, -1):
        word += mapping[num[i]]

    # now, check if word is a valid word.
    return word in valid_words
by Expert (34,270 points)
0 like 0 dislike

When typing numbers into a calculator, some numbers, when the calculator is flipped upside down, can be read as a word.

 

Some examples:
"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.

 

Some thoughts:

 

  • 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 string since 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.
by Expert (34,270 points)

Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .