[Solved] Number Patterns and Finding the Possible Smallest Numeric Value
Question: Given a pattern containing only Ns and Ms. N represents ascending and M represents descending, Each character (M or N) needs to display a sequence of numbers (2 numbers) explaining the ascending and descending order (for ex: 21 ->represents the ascending ->M). The second character in the patterns takes the last digit from the first character builds the sequence and so on. Please look at the example section below.
There could be multiple numbers satisfying the patterns. The goal is to find out the lowest numeric
the value following pattern.
Here are some constraints to this problem:
- Input can have a maximum of 8 characters.
- Output can have Digit from 1-9 and Digits can’t repeat.
- In case of no possible output or incorrect input value (like blank/ null/ NON M or N character), kindly return -1.
Input and Output Examples
Explanation: 2->1 shows descending and possible smallest numeric value. Even 65 or 75 can qualify, but
21 being the smallest numeric value is the correct answer.
Explanation: M represents descending 2->1. N represents ascending 1->4 (1 is coming from the last
character). M represent descending 4->3 (4 is coming from the last character sequence) — There would be
many numbers qualifying the pattern like 3142, 8796, 6241, etc. 2143 is the lowest numeric value for this
Number patterns and finding the possible smallest numeric value- This competitive coding challenge question was asked in one of the Goldman Sachs coding rounds.
Solution (Python Program)
This is really a tricky problem. I would recommend you to solve this problem by yourself first. Solve this competitive coding challenge question in any programming language you are familiar with, like C/C++, Java or Python.
Here is my Python solution to this problem.
#!/bin/python3 def findPossibleSmallestNumberMatchingPattern(pattern): maxTemp=0 valLast=0 lenMsg=len(pattern) if lenMsg>8: return -1 out= i=0 while i<lenMsg: numofM=0 if pattern[i]=='N': numofM=pattern[i+1: ].count('M') if i==0: maxTemp=numofM+2 valLast=valLast+1 out.append(str(valLast)) out.append(str(maxTemp)) valLast=maxTemp else: maxTemp=maxTemp+numofM+1 valLast=maxTemp out.append(str(valLast)) for k in range(numofM): valLast=valLast-1 out.append(str(valLast)) i=i+1 else: if i==0: j=i+1 numofM=0 #find the number of next M. while j<lenMsg and pattern[j]=='M': numofM += 1 j += 1 maxTemp=numofM+2 out.append(str(maxTemp)) out.append(str(maxTemp-1)) valLast=maxTemp-1 else: out.append(str(valLast-1)) valLast=valLast-1 i=i+1 return "".join(out) if __name__ == "__main__": print(findPossibleSmallestNumberMatchingPattern("MNM"))
We used various Python list methods like
append() and properties like indexing/slicing in this programming solution.
If you are preparing for a job in any product-based company, keep solving coding challenge questions. It will even help you to improve your programming skills.
1 is repeating. It should be 789654321.
I have a written a code that is partially correct bcoz it’s unable to solve the minimization problem but solving all other conditions.
For MNM output should be 2143 but I am getting 3241.
Please let me know if you are able to rewrite your code correctly.