- 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
Alpha-beta pruning gives the same answer as minimax while skipping branches that cannot change the result. It tracks two bounds: alpha, the best score MAX is already sure of, and beta, the best score MIN is already sure of.
The game tree is complete: every internal node has branching children, and all leaves are at the same depth. leaves lists the leaf scores from left to right. The root is a MAX node and the levels alternate MAX, MIN, MAX, …
Search children from left to right, starting with alpha = -∞ and beta = +∞:
- At a MAX node, after each child: best = max(best, value), then alpha = max(alpha, best).
- At a MIN node, after each child: best = min(best, value), then beta = min(beta, best).
- As soon as alpha ≥ beta, skip the node's remaining children.
Implement alpha_beta(leaves, branching).
Input
leaves: The leaf scores, left to right; the length is a power of branching.
branching: Children per internal node.
Output
Return [value, evaluated]: the minimax value of the root and how many leaves were evaluated.
Constraints
2 ≤ branching ≤ 5, length of leaves = branching^depth with 1 ≤ depth ≤ 8, at most 10,000 leaves
-1,000 ≤ each leaf ≤ 1,000
Examples
Example 1
Input:
leaves = [3, 12, 8, 2, 4, 6, 14, 5, 2]
branching = 3
Expected output:
[3, 7]
Explanation
The first MIN node sees 3, 12, 8 and returns 3, so alpha = 3. The second MIN node sees 2: beta = 2 ≤ alpha, so 4 and 6 are skipped. The third sees 14, 5, 2 and returns 2. The root value is max(3, 2, 2) = 3 after 7 of the 9 leaves.
Example 2
Input:
leaves = [5, 6, 7, 4]
branching = 2
Expected output:
[5, 4]
Explanation
The left MIN node evaluates 5 and 6 and returns 5, so alpha = 5. The right MIN node evaluates 7 (beta = 7), then 4 (beta = 4 ≤ 5). All 4 leaves are evaluated and the value is 5.
References
-
Example 1 Not runInput:
leaves = [3, 12, 8, 2, 4, 6, 14, 5, 2] branching = 3Expected output:[3, 7]Your output -
Example 2 Not runInput:
leaves = [5, 6, 7, 4] branching = 2Expected output:[5, 4]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 JavaScript Runner
Write, run, and debug JavaScript or Node.js online in a full-featured browser IDE. Work with multiple files and packages, save your workspace, and collaborate in real time.
🚀 339,661 total executions (665 this month)
Practice JavaScript with coding challenges
Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.
⚡ About This JavaScript (Node.js) Online Executor
The CodeUtility JavaScript Executor lets you write and run modern JavaScript code right in your browser - instantly and without any setup. It uses real Node.js environments to execute your code securely in a sandboxed container, supporting versions 16, 18, and 20.
This tool is perfect for testing Node.js snippets, exploring ES6+ syntax, or learning core JavaScript programming concepts such as loops, functions, arrays, promises, and async/await behavior.
You can also use it to validate algorithms, debug small scripts, or experiment with runtime logic without installing Node locally. It’s ideal for learners, backend developers, or anyone who needs a fast, browser-based way to run JavaScript code.
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.
💡 Node.js Basics Guide for Beginners
1. Declaring Variables and Constants
Use let or const for block-scoped variables. const is used for constants.
let x = 10;
const PI = 3.14;
let name = "Alice";
let isActive = true;
2. Conditionals (if / switch)
Use if, else if, else or switch for control flow.
let x = 2;
if (x === 1) {
console.log("One");
} else if (x === 2) {
console.log("Two");
} else {
console.log("Other");
}
switch (x) {
case 1:
console.log("One");
break;
case 2:
console.log("Two");
break;
default:
console.log("Other");
}
3. Loops
Use for, while, and forEach to iterate over data.
for (let i = 0; i < 3; i++) {
console.log(i);
}
let n = 3;
while (n > 0) {
console.log(n);
n--;
}
4. Arrays
Use arrays to store ordered lists of values. Access them with indexes.
let fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]);
console.log(fruits.length);
5. Array Manipulation
Use methods like push, pop, slice, and reverse.
fruits.push("kiwi");
fruits.pop();
console.log(fruits.slice(0, 2));
console.log(fruits.reverse());
let squares = [1, 2, 3, 4, 5].map(x => x * x);
console.log(squares);
6. Console Input/Output
Use console.log for output and readline module for input.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What's your name? ", name => {
console.log(`Hello, ${name}`);
rl.close();
});
7. Functions
Functions can be declared or expressed. Arrow functions are often used in modern JavaScript.
function greet(name) {
return `Hello, ${name}`;
}
const add = (a, b) => a + b;
console.log(greet("Alice"));
console.log(add(2, 3));
8. Objects (Maps)
Objects store key-value pairs. They are similar to dictionaries or maps.
let person = { name: "Bob", age: 25 };
console.log(person.name);
console.log(person["age"]);
// ES6 Map
const map = new Map();
map.set("a", 1);
console.log(map.get("a"));
9. Exception Handling
Use try, catch, and finally to handle errors safely.
try {
throw new Error("Something went wrong");
} catch (e) {
console.log(e.message);
} finally {
console.log("Cleanup if needed");
}
10. File I/O
Use Node.js's fs module to read and write files.
const fs = require("fs");
fs.writeFileSync("test.txt", "Hello File");
const data = fs.readFileSync("test.txt", "utf8");
console.log(data);
11. String Manipulation
Use methods like trim(), toUpperCase(), replace(), and split().
let text = " Hello World ";
console.log(text.trim());
console.log(text.toUpperCase());
console.log(text.replace("Hello", "Hi"));
console.log(text.split(" "));
12. Classes & Objects
ES6+ supports OOP with class syntax.
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hi, I'm ${this.name}`;
}
}
const p = new Person("Alice");
console.log(p.greet());
13. References
Objects and arrays in JavaScript are passed by reference.
function update(arr) {
arr.push("changed");
}
let data = ["original"];
update(data);
console.log(data); // ["original", "changed"]