Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Create a program to sort an array of 0s, 1s, and 2s in-place.

Hard

Problem Statement

Create a program to sort an array of 0s, 1s, and 2s in-place.

Sample Input

A hardcoded array, e.g., [2, 0, 2, 1, 1, 0].

Sample Output

Sorted array: [0, 0, 1, 1, 2, 2]

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
fun sortColors(nums: Array<Int>): Array<Int> { // Add your code here to sort the array in-place } fun main() { val nums = arrayOf(2, 0, 2, 1, 1, 0) println("Sorted array: ${sortColors(nums).joinToString(", ")}") }
Log in or sign up to run or submit your code.Log inSign up
Input
A hardcoded array, e.g., [2, 0, 2, 1, 1, 0].
Expected Output
Sorted array: [0, 0, 1, 1, 2, 2]