FastPrepNext Permutation
Problems/Next Permutation

Next Permutation

Morgan Stanley logoMorgan Stanley● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in the final technical and HR round of a Morgan Stanley intern onsite. The author links the Next Permutation problem. The next lexicographic arrangement, the wrap-around, and the change in place follow that problem. We chose the number limits and a function that returns the result so it can be checked.

Our version is about 95% the same as the reported question.

Given an integer array nums, rearrange it into the next lexicographically greater distinct value permutation. Lexicographic order compares the first position where two arrays differ. If no greater arrangement exists, rearrange it into ascending order.

Modify nums in place using constant extra working memory. Repeated values are allowed. For this exercise, return the modified array so the judge can observe the mutation. The returned representation is an observation adapter; do not build a separate working array to compute the permutation.

Function

nextPermutation(nums: int[]) → int[]

Examples

Example 1

nums = [1,4,3,2]return = [2,1,3,4]

The suffix [4,3,2] is its largest arrangement, so the first 1 must increase. The smallest available greater value is 2. Put the remaining values [1,3,4] in ascending order, giving [2,1,3,4].

Example 2

nums = [3,2,2]return = [2,2,3]

The input [3,2,2] is the greatest arrangement of these values. Wrap to the smallest arrangement [2,2,3].

Constraints

  • For this exercise, assume 1 <= nums.length <= 100.
  • For this exercise, assume 0 <= nums[i] <= 100.

More Morgan Stanley Onsite Interview questions

  • Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
  • Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
  • Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
  • Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
  • Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
  • Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
  • LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
  • All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
  • Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
  • 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
  • Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
  • Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Rotate an Array Right by K StepsQualys · ONSITE INTERVIEWQualys interview questions
  • Find the Duplicate NumberWalmart Labs · ONSITE INTERVIEW
  • Two or Three Sum ExistenceGoogle · ONSITE INTERVIEWGoogle interview questions

Interview reports that mention this question

  • Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)

Morgan Stanley interview experiences

  • Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
  • Morgan Stanley New Grad Technology Analyst: 2 Experiences
  • Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
  • Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public int[] nextPermutation(int[] nums) {
        // Modify nums in place and return it.
    }

}
Checking account…