BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Attention and transformers17 of 23 in topic

407. Rotate a vector by its position (RoPE)

HardAttention and transformers Pro
Asked atByteDanceMistralOpenAI

Rotary position embeddings (RoPE) tell attention where each token sits by rotating its query and key vectors. Pairs of numbers are treated as points on a plane and turned by an angle that depends on the position.

Write apply_rope(x, pos, base=10000.0) where x is a 1D array of even length d and pos is an integer position.

For each pair index i from 0 to d/2 - 1:
- theta_i = base ** (-2 * i / d)
- angle = pos * theta_i
- take the pair (x[2i], x[2i+1]) and rotate it: new_even = x[2i]*cos(angle) - x[2i+1]*sin(angle), new_odd = x[2i]*sin(angle) + x[2i+1]*cos(angle)

Return the rotated array. Position 0 leaves x unchanged, and rotation never changes the vector's length.

Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.

Functiondef apply_rope(x, pos, base=10000.0)

Example 1

Input: x = [1.0, 2.0, 3.0, 4.0], pos = 0

Output: [1.0, 2.0, 3.0, 4.0]

Example 2

Input: x = [1.0, 0.0], pos = 1

Output: [0.540302, 0.841471]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Each consecutive pair of numbers is a 2D point that gets rotated by its own angle, and all pairs can be handled at once.

Hint 2

Build the pair indices i = 0..d/2 - 1, compute the angles pos * base ** (-2 * i / d), take cos and sin, then apply the 2D rotation to the even and odd slices together.

Hint 3

Read both slices x[0::2] and x[1::2] before writing anything, and write results into a new array: out[0::2] = xe * c - xo * s and out[1::2] = xe * s + xo * c.

Understand the idea

RoPE tells attention where a token sits by rotating its query and key vectors. Picture each pair of numbers as a clock hand: position decides how far the hand turns. Each pair i has its own speed theta_i = base^(-2i/d), so early pairs spin fast and later pairs spin slowly. The angle for a pair is pos * theta_i.

Worked example

d = 2, so there is one pair with theta_0 = 1. Take x = [1, 0] at pos = 1, so angle = 1 radian, cos = 0.540, sin = 0.841. New even = 1*0.540 - 0*0.841 = 0.540, new odd = 1*0.841 + 0*0.540 = 0.841. The length is still 1. For d = 4, the second pair has theta_1 = 10000^(-0.5) = 0.01, so it turns 100 times slower.

for pair (a, b) and angle t, output (a cos t - b sin t, a sin t + b cos t)

Position 0 means no change. Llama and most modern LLMs use RoPE.

Topics
Attention and transformersLLM architecture track
Companies3
  • ByteDance the topic appears in interview reports ByteDance Machine Learning Engineer Interview Process
  • Mistral the topic appears in interview reports Mistral AI Interview Guide 2026: European Frontier Lab, Open-Weight Strategy, MoE, and La Plateforme
  • OpenAI the topic appears in interview reports OpenAI Research Engineer Interview Guide (Dataford)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Run a full pre-norm transformer block forward passHard
  • Backpropagate through scaled dot-product attentionHard
  • Compute the gradient of a tied input and output embeddingHard
  • Convert a GPT block to a Llama-style block (RMSNorm, RoPE, SwiGLU)Hard
All Attention and transformers problems
Report
407 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def apply_rope(x, pos, base=10000.0):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS