- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AINeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Learn MatplotlibLine, bar, scatter and pie charts with live visualizers Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++ Coding for Kids Learn to code by playing!
- ChallengesNew
- For TeachersNew
- Updates
- Buy me a coffee
A genetic algorithm improves a population of candidate solutions the way evolution does: select fit parents, combine them, and add random mutations. Here the candidates are bit strings and fitness is the number of 1s (the OneMax problem).
To make it testable, every random choice is given. With N strings (N is even), build N children in pairs. For pair k = 0, 1, …, N/2 - 1:
- Selection: parent1 wins tournament
tournaments[2k]and parent2 winstournaments[2k + 1]. A tournament[i, j]is won by whichever ofpopulation[i]andpopulation[j]has more 1s; on a tie,population[i]wins. - Crossover at c =
crossover_points[k]: child 2k = parent1[0:c] + parent2[c:] and child 2k + 1 = parent2[0:c] + parent1[c:].
Then apply each mutation [child, position] in order by flipping that bit of that child.
Implement ga_generation(population, tournaments, crossover_points, mutations).
Input
population: N bit strings of equal length L.
tournaments: N pairs of population indices.
crossover_points: N/2 cut points, each between 1 and L - 1.
mutations: [child, position] pairs (may be empty).
Output
Return the N children in order.
Constraints
2 ≤ N ≤ 200 (N even), 2 ≤ L ≤ 64
0 ≤ number of mutations ≤ 1,000
Examples
Example 1
Input:
population = ["0000", "1111", "1100", "0011"]
tournaments = [
[0, 1],
[2, 3],
[1, 2],
[0, 3]
]
crossover_points = [2, 1]
mutations = [
[0, 0]
]
Expected output:
["0100", "1111", "1011", "0111"]
Explanation
Pair 0: "1111" beats "0000", and "1100" ties "0011" so the first one wins. Cutting after 2 bits gives "1100" and "1111". Pair 1: parents "1111" and "0011", cut after 1 bit: "1011" and "0111". Finally child 0 has bit 0 flipped: "0100".
Example 2
Input:
population = ["10", "01"]
tournaments = [
[0, 1],
[1, 0]
]
crossover_points = [1]
mutations = []
Expected output:
["11", "00"]
Explanation
Both strings have one 1, so each tournament is won by its first index: parent1 = "10" and parent2 = "01". Cutting after 1 bit gives "11" and "00".
References
-
Example 1 Not runInput:
population = ["0000", "1111", "1100", "0011"] tournaments = [ [0, 1], [2, 3], [1, 2], [0, 3] ] crossover_points = [2, 1] mutations = [ [0, 0] ]Expected output:["0100", "1111", "1011", "0111"]Your output -
Example 2 Not runInput:
population = ["10", "01"] tournaments = [ [0, 1], [1, 0] ] crossover_points = [1] mutations = []Expected output:["11", "00"]Your output
4 hidden tests also run when you submit. If your code fails one, you'll see that test. Solve the challenge to see them all.
Online Kotlin Compiler
Write, compile, run, and debug Kotlin code online in a full-featured browser IDE. Work with multiple files, save your workspace, and collaborate in real time.
🚀 71,457 total executions (576 this month)
Practice Kotlin with coding challenges
Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.
🧩 About This Kotlin Online Executor
The CodeUtility Kotlin Executor lets you write, compile, and run Kotlin code directly in your browser - no installation, IDE, or SDK setup required. It uses real Kotlin compilers running in isolated sandboxes for a safe and authentic execution experience.
This tool supports multiple Kotlin versions - 1.6, 1.9, and 2.1 - so you can explore syntax updates, test code compatibility, and experiment with modern language features like coroutines, data classes, and sealed interfaces.
It’s ideal for students learning Kotlin basics, Android developers experimenting with snippets, or backend engineers testing logic in a JVM environment without leaving the browser.
The environment runs each snippet inside a secure containerized runtime, ensuring consistent performance and safe code execution.
More than a code runner
Use a multi-file editor, an isolated execution terminal, runtime-specific libraries, persistent workspaces, sharing, and real-time collaboration from the same executor.
How to use this executor?
Choose a runtime, open or create a file, write your code, and select Run. Output appears in the terminal, while signed-in users can keep files in a workspace, install libraries, and collaborate through shares.
- Select a version and edit the current file or open another workspace file.
- Run the code, provide input in the terminal when requested, and review saved run history.
- Use libraries, workspace tools, sharing, and chat when your task needs more than one snippet.
💡 Kotlin Basics Guide for Beginners
1. Declaring Variables and Constants
Kotlin uses val for immutable variables and var for mutable ones.
val name: String = "Alice"
var age: Int = 30
val pi = 3.14 // type inferred
// Constants
const val MAX_USERS = 100
2. Conditionals (if / when)
Use if expressions or when for multiple branches.
val x = 2
if (x == 1) {
println("One")
} else if (x == 2) {
println("Two")
} else {
println("Other")
}
when (x) {
1 -> println("One")
2 -> println("Two")
else -> println("Other")
}
3. Loops
Use for, while, and do-while for iteration.
for (i in 0..2) {
println(i)
}
var n = 3
while (n > 0) {
println(n)
n--
}
4. Arrays
Kotlin supports arrays using the arrayOf() function.
val numbers = arrayOf(10, 20, 30)
println(numbers[1])
5. List Manipulation
Use mutableListOf for dynamic lists.
val nums = mutableListOf(1, 2, 3)
nums.add(4)
nums.removeAt(0)
for (n in nums) {
print("$n ")
}
6. Console Input/Output
Use readLine() for input and println() for output.
print("Enter your name: ")
val name = readLine()
println("Hello, $name")
7. Functions
Functions use fun keyword with optional return type.
fun add(a: Int, b: Int): Int {
return a + b
}
println(add(3, 4))
8. Maps
mutableMapOf stores key-value pairs.
val ages = mutableMapOf("Alice" to 30)
println(ages["Alice"])
9. Exception Handling
Use try, catch, and finally to handle errors.
try {
val result = 10 / 0
} catch (e: ArithmeticException) {
println("Error: ${e.message}")
}
10. File I/O
Use File from java.io for file operations.
import java.io.File
File("file.txt").writeText("Hello File")
val text = File("file.txt").readText()
println(text)
11. String Manipulation
Kotlin strings support many methods and interpolation.
val text = "Hello World"
println(text.length)
println(text.substring(0, 5))
println(text.contains("World"))
12. Classes & Objects
Kotlin classes are concise and support default constructors.
class Person(val name: String) {
fun greet() = println("Hi, I'm $name")
}
val p = Person("Alice")
p.greet()
13. Null Safety
Kotlin distinguishes nullable and non-nullable types. Use ? and ?: for safety.
val name: String? = null
println(name?.length ?: 0)