- 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
Backpropagation applies the chain rule layer by layer, from the output back to the inputs. The network has n inputs, h hidden neurons and one output, all with sigmoid activation (σ(z) = 1 / (1 + e^(-z))):
- hidden: a_j = σ(inputs · hidden_weights[j] + hidden_biases[j])
- output: y = σ(a · output_weights + output_bias)
- loss: L = (y - target)²
Gradients:
- delta_out = 2 × (y - target) × y × (1 - y)
- Output layer: dL/doutput_weights[j] = delta_out × a_j and dL/doutput_bias = delta_out
- delta_j = delta_out × output_weights[j] × a_j × (1 - a_j)
- Hidden layer: dL/dhidden_weights[j][i] = delta_j × inputs[i] and dL/dhidden_biases[j] = delta_j
Implement backprop_gradients(inputs, hidden_weights, hidden_biases, output_weights, output_bias, target).
Output
Return a matrix with h + 1 rows:
- Row j (for each hidden neuron j, in order): the n gradients of
hidden_weights[j], then the gradient ofhidden_biases[j]. - Last row: the h gradients of
output_weights, then the gradient ofoutput_bias.
Constraints
1 ≤ n, h ≤ 20
-5 ≤ each input, weight and bias ≤ 5
0 ≤ target ≤ 1
Examples
Example 1
Input:
inputs = [1.0]
hidden_weights = [
[0.0]
]
hidden_biases = [0.0]
output_weights = [0.0]
output_bias = 0.0
target = 1.0
Expected output:
[
[0.0, 0.0],
[-0.125, -0.25]
]
Explanation
a = σ(0) = 0.5 and y = σ(0) = 0.5. delta_out = 2 × (0.5 - 1) × 0.25 = -0.25. Output row: [-0.25 × 0.5, -0.25] = [-0.125, -0.25]. delta_1 = -0.25 × 0 × 0.25 = 0, so the hidden row is [0, 0]: a zero output weight blocks the gradient.
Example 2
Input:
inputs = [1.0, 0.0]
hidden_weights = [
[0.0, 1.0],
[0.0, -1.0]
]
hidden_biases = [0.0, 0.0]
output_weights = [1.0, 1.0]
output_bias = -1.0
target = 0.0
Expected output:
[
[0.0625, 0.0, 0.0625],
[0.0625, 0.0, 0.0625],
[0.125, 0.125, 0.25]
]
Explanation
Both hidden values are σ(0) = 0.5, so y = σ(0.5 + 0.5 - 1) = 0.5 and delta_out = 2 × 0.5 × 0.25 = 0.25. Output row: [0.125, 0.125, 0.25]. Each delta_j = 0.25 × 1 × 0.25 = 0.0625, giving hidden rows [0.0625, 0, 0.0625] (the second input is 0).
References
-
Example 1 Not runInput:
inputs = [1.0] hidden_weights = [ [0.0] ] hidden_biases = [0.0] output_weights = [0.0] output_bias = 0.0 target = 1.0Expected output:[ [0.0, 0.0], [-0.125, -0.25] ]Your output -
Example 2 Not runInput:
inputs = [1.0, 0.0] hidden_weights = [ [0.0, 1.0], [0.0, -1.0] ] hidden_biases = [0.0, 0.0] output_weights = [1.0, 1.0] output_bias = -1.0 target = 0.0Expected output:[ [0.0625, 0.0, 0.0625], [0.0625, 0.0, 0.0625], [0.125, 0.125, 0.25] ]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 Rust Compiler
Write, compile, run, and debug Rust code online in a full-featured browser IDE. Work with multiple files, save your workspace, and share or collaborate in real time.
🚀 252,702 total executions (135 this month)
Practice Rust with coding challenges
Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.
🦀 About This Rust Online Executor
The CodeUtility Rust Executor lets you write and run Rust code directly in your browser - no installation, toolchain setup, or local environment required. It’s powered by a secure sandbox that supports real Rust compiler versions 1.70, 1.72, 1.73, and 1.74.
This tool compiles and executes your Rust programs in the cloud using a genuine rustc compiler,
ensuring accurate and consistent results. You can experiment with variables, ownership, borrowing,
pattern matching, traits, generics, and other modern Rust features with ease.
It’s ideal for learning Rust’s core concepts - especially memory safety, error handling, and concurrency - without needing to install Cargo or any local setup. Everything runs directly in your browser within seconds.
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.
💡 Rust Basics & Examples You Can Try Above
1. Declaring Variables and Constants
Use let to declare variables. Use const for constants and mut for mutable variables.
let x = 10;
let pi: f64 = 3.14;
let name = "Alice";
let is_active = true;
const MAX_USERS: u32 = 100;
2. Conditionals (if / match)
Rust uses if for conditions and match for pattern matching.
let x = 2;
if x == 1 {
println!("One");
} else if x == 2 {
println!("Two");
} else {
println!("Other");
}
match x {
1 => println!("One"),
2 => println!("Two"),
_ => println!("Other"),
}
3. Loops
Rust supports for, while, and infinite loop.
for i in 0..3 {
println!("{}", i);
}
let mut n = 3;
while n > 0 {
println!("{}", n);
n -= 1;
}
4. Arrays
Fixed-size arrays in Rust use square brackets. Use slices for views.
let nums = [10, 20, 30];
println!("{}", nums[1]);
5. Vectors (Dynamic Arrays)
Vec is used for dynamic arrays.
let mut fruits = vec!["apple", "banana"];
fruits.push("cherry");
fruits.pop();
for fruit in &fruits {
println!("{}", fruit);
}
6. Console Input/Output
Use println! for output and std::io for input.
use std::io;
let mut name = String::new();
println!("Enter your name:");
io::stdin().read_line(&mut name).unwrap();
println!("Hello, {}", name.trim());
7. Functions
Functions use fn and can return values using ->.
fn greet(name: &str) -> String {
format!("Hello, {}", name)
}
println!("{}", greet("Alice"));
8. HashMaps
Use std::collections::HashMap for key-value data.
use std::collections::HashMap;
let mut person = HashMap::new();
person.insert("name", "Bob");
println!("{}", person["name"])
9. Error Handling
Rust handles errors using Result and match or unwrap().
fn fail() -> Result<(), String> {
Err("Something went wrong".to_string())
}
match fail() {
Ok(_) => println!("Success"),
Err(e) => println!("{}", e),
}
10. File I/O
Use std::fs to read and write files.
use std::fs;
fs::write("file.txt", "Hello File").unwrap();
let content = fs::read_to_string("file.txt").unwrap();
println!("{}", content);
11. String Manipulation
Strings in Rust are heap-allocated and support many methods.
let mut text = String::from(" Hello Rust ");
println!("{}", text.trim());
println!("{}", text.to_uppercase());
text = text.replace("Hello", "Hi");
println!("{}", text);
12. Structs & Methods
Rust uses struct for data types and impl for methods.
struct Person {
name: String,
}
impl Person {
fn greet(&self) -> String {
format!("Hi, I'm {}", self.name)
}
}
let p = Person { name: "Alice".to_string() };
println!("{}", p.greet());
13. References & Borrowing
Rust uses & to borrow values and &mut for mutable references.
fn update(x: &mut i32) {
*x += 1;
}
let mut value = 10;
update(&mut value);
println!("{}", value);