- 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
Multi-head attention lets a Transformer attend to different things at once. With input X (n tokens × d columns) and weight matrices of size d × d:
- Q = X × w_q, K = X × w_k, V = X × w_v
- Split the d columns into
num_headsheads of d_k = d / num_heads consecutive columns (head h uses columns h×d_k to (h+1)×d_k - 1). - For each head, run scaled dot-product attention on its columns: for token i, score_j = (Q_i · K_j) / √d_k over that head's columns, softmax over all n tokens j, and output = Σ weight_j × V_j (that head's columns).
- Put the heads' outputs back in their columns (concatenate) and multiply by w_o.
Implement multi_head_attention(x, w_q, w_k, w_v, w_o, num_heads).
Input
x: n × d input matrix.
w_q, w_k, w_v, w_o: d × d weight matrices.
num_heads: Number of heads (d is divisible by it).
Output
Return the n × d output matrix.
Constraints
1 ≤ n ≤ 30, 1 ≤ d ≤ 32, 1 ≤ num_heads ≤ d and d is divisible by num_heads
-5 ≤ each value ≤ 5
Examples
Example 1
Input:
x = [
[1.0, 0.0],
[0.0, 1.0]
]
w_q = [
[0.0, 0.0],
[0.0, 0.0]
]
w_k = [
[1.0, 0.0],
[0.0, 1.0]
]
w_v = [
[1.0, 0.0],
[0.0, 1.0]
]
w_o = [
[1.0, 0.0],
[0.0, 1.0]
]
num_heads = 1
Expected output:
[
[0.5, 0.5],
[0.5, 0.5]
]
Explanation
w_q is all zeros, so every score is 0 and each token averages the value rows: V = X, so both rows become [0.5, 0.5].
Example 2
Input:
x = [
[2.0, 4.0]
]
w_q = [
[1.0, 0.0],
[0.0, 1.0]
]
w_k = [
[1.0, 0.0],
[0.0, 1.0]
]
w_v = [
[1.0, 0.0],
[0.0, 1.0]
]
w_o = [
[0.0, 1.0],
[1.0, 0.0]
]
num_heads = 2
Expected output:
[
[4.0, 2.0]
]
Explanation
With a single token each head attends only to itself, so the concatenated output is V = [2, 4]. w_o swaps the two columns: [4, 2].
References
-
Example 1 Not runInput:
x = [ [1.0, 0.0], [0.0, 1.0] ] w_q = [ [0.0, 0.0], [0.0, 0.0] ] w_k = [ [1.0, 0.0], [0.0, 1.0] ] w_v = [ [1.0, 0.0], [0.0, 1.0] ] w_o = [ [1.0, 0.0], [0.0, 1.0] ] num_heads = 1Expected output:[ [0.5, 0.5], [0.5, 0.5] ]Your output -
Example 2 Not runInput:
x = [ [2.0, 4.0] ] w_q = [ [1.0, 0.0], [0.0, 1.0] ] w_k = [ [1.0, 0.0], [0.0, 1.0] ] w_v = [ [1.0, 0.0], [0.0, 1.0] ] w_o = [ [0.0, 1.0], [1.0, 0.0] ] num_heads = 2Expected output:[ [4.0, 2.0] ]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 Go Compiler
Write, compile, run, and debug Go code online in a full-featured browser IDE. Work with multiple files, save your workspace, and collaborate in real time - no setup required.
🚀 206,970 total executions (799 this month)
Practice Go with coding challenges
Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.
🐹 About This Go Online Executor
The CodeUtility Go Executor lets you write and run Go code directly in your browser - no installation, setup, or local environment required. It’s powered by a secure sandbox that supports real Go compiler versions 1.19, 1.20, and 1.21.
This tool compiles and executes real Go programs in the cloud, giving you an authentic Go environment. You can test short snippets, experiment with packages, and explore Go’s powerful concurrency features using goroutines and channels.
It’s perfect for learning and practicing core Go concepts such as variables, functions, structs, slices, maps, interfaces, and error handling - all without installing any tools locally.
Whether you’re exploring Go for the first time, testing backend logic, or learning how concurrency works, the CodeUtility Go Executor offers a clean, fast, and browser-based way to run Go code instantly.
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.
💡 Go Basics & Examples You Can Try Above
1. Declaring Variables and Constants
Use var or := to declare variables. Use const for constants.
package main
var x int = 10
const Pi = 3.14
func main() {
name := "Alice"
isActive := true
println(x, Pi, name, isActive)
}
2. Conditionals (if / switch)
Go supports if-else and switch statements with clean syntax.
x := 2
if x == 1 {
println("One")
} else if x == 2 {
println("Two")
} else {
println("Other")
}
switch x {
case 1:
println("One")
case 2:
println("Two")
default:
println("Other")
}
3. Loops
Go uses only the for loop, but it can act like a while loop.
for i := 0; i < 3; i++ {
println(i)
}
n := 3
for n > 0 {
println(n)
n--
}
4. Arrays
Arrays have fixed size. Use slices for dynamic lists.
var nums = [3]int{10, 20, 30}
println(nums[1])
5. Slice Manipulation
Use slices and the built-in append, len, copy, and slicing syntax.
fruits := []string{"apple", "banana"}
fruits = append(fruits, "cherry")
fruits = fruits[1:] // slice
println(len(fruits))
for _, fruit := range fruits {
println(fruit)
}
6. Console Input/Output
Use fmt.Print, fmt.Scan, and fmt.Println.
import "fmt"
var name string
fmt.Print("Enter your name: ")
fmt.Scan(&name)
fmt.Println("Hello", name)
7. Functions
Functions are declared with func and can return multiple values.
func greet(name string) string {
return "Hello, " + name
}
message := greet("Alice")
println(message)
8. Maps
Maps are key-value stores. Declare with make().
ages := map[string]int{"Alice": 30}
ages["Bob"] = 25
println(ages["Alice"])
9. Error Handling
Go uses multiple return values for errors instead of exceptions.
import "errors"
func fail() error {
return errors.New("something went wrong")
}
err := fail()
if err != nil {
println(err.Error())
}
10. File I/O
Use os and io/ioutil packages to read/write files.
import (
"fmt"
"os"
"io/ioutil"
)
ioutil.WriteFile("file.txt", []byte("Hello File"), 0644)
data, _ := ioutil.ReadFile("file.txt")
fmt.Println(string(data))
11. String Manipulation
Use the strings package for string operations.
import "strings"
text := " Hello World "
println(strings.TrimSpace(text))
println(strings.ToUpper(text))
println(strings.ReplaceAll(text, "Hello", "Hi"))
words := strings.Split(text, " ")
fmt.Println(words)
12. Structs & Objects
Use structs to define custom types with methods.
type Person struct {
Name string
}
func (p Person) Greet() string {
return "Hi, I'm " + p.Name
}
p := Person{Name: "Alice"}
println(p.Greet())
13. References (Pointers)
Go supports pointers using * and &.
x := 10
ptr := &x
*ptr = 20
println(x) // 20