Asked inWalmart,SDE-2

Maximum Number With Single Swap

You are given an array of N elements that represent the digits of a number. You can perform one swap operation to exchange the values at any two indices. Your task is to determine the maximum possible number that can be achieved by performing at most one swap.

Example:

Input:
array = [1,3,2,7]
Output:
[7,3,2,1]
Explanation:

The input array represents the number 1327. After exploring possible swaps, swapping indices 1 and 4 yields the maximum number 7321. Thus, the output array is [7, 3, 2, 1].

Constraints:

  • 1 <= T <= 100
  • 2 <= N <= 104
  • 0 <= A[i] <= 9
  • The array can include leading zeros.
Note:

For each test case, the output should be printed where elements are space-separated, on a new line for each test case.

AnswerBot
4mo

Given an array of digits, find the maximum number that can be achieved by performing at most one swap.

  • Iterate through the array to find the maximum digit.

  • If the maximum digit is already at the beginni...read more

Help your peers!
Select
Add answer anonymously...

Walmart SDE-2 interview questions & answers

A SDE-2 was asked Q. Bipartite Graph Problem Statement Determine if a given graph is bipartite. A gra...read more
A SDE-2 was asked Q. Can you explain the DHCP Protocol?
A SDE-2 was asked Q. Can you explain the TCP/IP protocol?

Popular interview questions of SDE-2

A SDE-2 was asked Q1. Bipartite Graph Problem Statement Determine if a given graph is bipartite. A gra...read more
A SDE-2 was asked Q2. Can you explain the DHCP Protocol?
A SDE-2 was asked Q3. Can you explain the TCP/IP protocol?
Walmart SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits