Sort a list ascending when its length is even and descending when its length is odd

Quick Overview

Given a list of integers, return it sorted in ascending order when the list has an even length and in descending order when the length is odd. It checks parity logic, use of a built-in sort with a reverse order, and edge cases such as an empty list and duplicate values.

Sort a list ascending when its length is even and descending when its length is odd

Company: DXC Technology

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a list of integers `nums`, return the list sorted based on its length: - If the length of `nums` is even, return the list sorted in ascending order. - If the length of `nums` is odd, return the list sorted in descending order. For example, `[3,1,2]` has length 3 (odd), so it should be returned as `[3,2,1]`. The list `[3,1,4,2]` has length 4 (even), so it should be returned as `[1,2,3,4]`. ### Function Signature ```python def conditional_sort(nums: list[int]) -> list[int]: ``` ### Rules - The decision depends only on the length of `nums`, never on the values. - Duplicate values are kept, so the output has the same length as the input. - An empty list has length 0, which is even, so the result is an empty list. ### Constraints - `0 <= len(nums) <= 10^4` - `-10^9 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [3,1,2] Output: [3,2,1] ``` The list has length 3 (odd), so it is sorted in descending order. ```text Input: nums = [3,1,4,2] Output: [1,2,3,4] ``` The list has length 4 (even), so it is sorted in ascending order. ```text Input: nums = [5,-2,5,0,7] Output: [7,5,5,0,-2] ``` The list has length 5 (odd), so it is sorted in descending order. Both copies of 5 stay in the result, and the negative value ends up last.

Overview: Given a list of integers, return it sorted in ascending order when the list has an even length and in descending order when the length is odd. It checks parity logic, use of a built-in sort with a reverse order, and edge cases such as an empty list and duplicate values.

DXC Technology logo
DXC Technology
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a list of integers nums, return the list sorted based on its length:

  • If the length of nums is even, return the list sorted in ascending order.
  • If the length of nums is odd, return the list sorted in descending order.

For example, [3,1,2] has length 3 (odd), so it should be returned as [3,2,1]. The list [3,1,4,2] has length 4 (even), so it should be returned as [1,2,3,4].

Function Signature

def conditional_sort(nums: list[int]) -> list[int]:

Rules

  • The decision depends only on the length of nums , never on the values.
  • Duplicate values are kept, so the output has the same length as the input.
  • An empty list has length 0, which is even, so the result is an empty list.

Constraints

  • 0 <= len(nums) <= 10^4
  • -10^9 <= nums[i] <= 10^9

Examples

Input: nums = [3,1,2]
Output: [3,2,1]

The list has length 3 (odd), so it is sorted in descending order.

Input: nums = [3,1,4,2]
Output: [1,2,3,4]

The list has length 4 (even), so it is sorted in ascending order.

Input: nums = [5,-2,5,0,7]
Output: [7,5,5,0,-2]

The list has length 5 (odd), so it is sorted in descending order. Both copies of 5 stay in the result, and the negative value ends up last.