PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of permutations and lexicographic ordering, in-place array manipulation, and algorithmic reasoning about time and space constraints, including handling duplicates and edge cases.

  • medium
  • Meta
  • Coding & Algorithms
  • Machine Learning Engineer

Compute the next lexicographic permutation

Company: Meta

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Given an integer array `nums` representing a permutation of its elements, modify the array **in-place** to produce the **next lexicographically greater** permutation. - If such a permutation exists, rearrange `nums` into that next greater ordering. - If no greater permutation exists (i.e., `nums` is in non-increasing order), rearrange `nums` into the **lowest possible order** (sorted ascending). ## Input - `nums`: an array of integers (may contain duplicates). ## Output - No return value; update `nums` in-place. ## Requirements - Use **O(1)** extra space. - Aim for **O(n)** time. ## Examples 1. `nums = [1,2,3]` → after update: `[1,3,2]` 2. `nums = [3,2,1]` → after update: `[1,2,3]` 3. `nums = [1,1,5]` → after update: `[1,5,1]` ## Notes / Edge Cases to consider - Array length `n` can be 0, 1, or larger. - Duplicates may exist. - Strictly increasing, strictly decreasing, and “plateau” patterns (e.g., `[2,2,1]`).

Quick Answer: This question evaluates understanding of permutations and lexicographic ordering, in-place array manipulation, and algorithmic reasoning about time and space constraints, including handling duplicates and edge cases.

Return the next lexicographic permutation of nums, or the lowest sorted order if none exists.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([1,2,3],)

Expected Output: [1, 3, 2]

Explanation: Prompt example 1.

Input: ([3,2,1],)

Expected Output: [1, 2, 3]

Explanation: Wrap to lowest order.

Input: ([1,1,5],)

Expected Output: [1, 5, 1]

Explanation: Duplicates.

Input: ([],)

Expected Output: []

Explanation: Empty array.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)