• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

[Solved] Number Patterns and Finding the Possible Smallest Numeric Value

Aniruddha Chaudhari/13230/1
CodePython

Problem Statement

Question: Given a pattern containing only Ns and Ms. N represent ascending and M represent 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 the 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

Example 1:

Input: M

Output: 21

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.

Example 2:

Input: MNM

Output: 2143

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 last character sequence) — There would be
many numbers qualifying the pattern like 3142, 8796, 6241, etc. 2143 is the lowest numeric value for this
pattern sequence.

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"))

Output:

2143

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.

Python Interview Questions eBook

coding challengePython
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Ranjan Sharma
    December 12, 2020 at 3:39 pm
    input = NNMMMMMM
    output = 187654321

    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.

Leave a Reply Cancel reply

Why?

Why Competitive Programming Important?

Coding Challenges for Practice

  1. Count Common Factor
  2. Does it Divide
  3. Sum of Sub Arrays
  4. Pair of Desired Sum
  5. Remove Duplicate Char from String
  6. Sort String by Char Freq (Python)
  7. Sort String by Char Freq (Java)
  8. Split Array into Equal Sum Subarray
  9. Validate IP Address
  10. Validate PAN Card Number
  11. Validate Sudoku
  12. Sort Circular Rotated Array
  13. Min Arrow to Burst Bubbles
  14. Min Cost to Paint All Houses [Amazon]
  15. HourGlass with Largest Sum
  16. Max Profit by Buying/Selling Stocks
  17. Hailstone Sequence
  18. Reverse String without affecting Special Characters
  19. Secure Conversation by Encry/Decry
  20. Special Elements in Matrix
  21. Next Greater No with Same set of Digits
  22. Smallest Subarray with Sum Greater than Given Number
  23. Group Anagrams
  24. Find Duplicates in Array in O(n)
  25. Find Two Unique Numbers from Array in O(n)
  26. Number Patterns & Finding Smallest Number
  27. First Unique Element in a Stream
  28. Flip Equivalent Binary Trees [TeachMint]
  29. Minimum Cost of Merging Files [Amazon]
  30. Minimum Distance for Truck to Deliver Order [Amazon]
  31. Order Task for Given Dependencies
  32. Design Music Player
  33. Multilevel Parking System Design
  34. Minimum Coins Required
  35. Max Sum Subarray
  36. Max Avg Sum of Two Subsequences
  37. Merge Overlapping Intervals
  38. Longest Balanced Subarray
  39. Longest Path in a Weighted Tree
  40. Generate Balanced Parentheses
  41. PostOrder Traversal Without Recursion

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard