Next Greater Number Problem Statement
Given a string S
which represents a number, determine the smallest number strictly greater than the original number composed of the same digits. Each digit's frequency from 0-9 must match the original.
Input:
The first line is an integer T, the number of test cases.
Each test case contains one line with the string S.
Output:
For each test case, output the smallest number strictly greater using the same set of digits or -1 if it doesn't exist.
Example:
If given S = '56789'
, the output should be 56798
. Note that 56790
is also greater but uses different digits.
Constraints:
1 ≤ T ≤ 100
1 ≤ len(S) ≤ 104
- The function has a time limit of 1 second.
Note:
The input string is non-empty with no leading zeros. Implement the function; printing is handled elsewhere.

AnswerBot
4mo
Given a number represented as a string, find the smallest number greater than the original with the same digits.
Iterate from right to left to find the first digit that can be swapped with a smaller di...read more
Help your peers!
Add answer anonymously...
Morgan Stanley Digital Technology Intern interview questions & answers
A Digital Technology Intern was asked Q. Boundary Traversal of a Binary Tree Given a binary tree of integers, your task i...read more
A Digital Technology Intern was asked Q. Minimum Number of Platforms Problem Your task is to determine the minimum number...read more
A Digital Technology Intern was asked Q. Next Greater Number Problem Statement Given a string S which represents a number...read more
Popular interview questions of Digital Technology Intern
A Digital Technology Intern was asked Q1. Boundary Traversal of a Binary Tree Given a binary tree of integers, your task i...read more
A Digital Technology Intern was asked Q2. Minimum Number of Platforms Problem Your task is to determine the minimum number...read more
A Digital Technology Intern was asked Q3. Next Greater Number Problem Statement Given a string S which represents a number...read more
>
Morgan Stanley Digital Technology Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

