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

487. Route each token to its top 2 experts in a mixture of experts

MediumEfficient architectures Pro
Asked atMistral

A mixture-of-experts (MoE) layer has many small feed-forward networks (experts) but runs only a few per token. A router scores every expert for every token, keeps the best k, and mixes their outputs with weights that sum to 1.

Write moe_route(logits, k=2) where logits has shape (tokens, experts). Return (idx, weights):

- idx: shape (tokens, k), the indices of the k highest logits per token, highest first. On a tie, the lower expert index wins.
- weights: shape (tokens, k), a softmax over only those k logits (subtract the max first).

Then write expert_load(idx, n_experts): a list with how many tokens were sent to each expert. Load balance matters: an expert that gets every token becomes the bottleneck.

Functiondef moe_route(logits, k=2)

Example 1

Input: np.asarray(moe_route(L)[0]).tolist()

Output: [[1, 2], [2, 3]]

Example 2

Input: moe_route(L)[1][0]

Output: [0.731059, 0.268941]

Constraints

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

A stable argsort of -logits per row gives top-k with ties to the lower index.

Hint 2

Softmax only over the k chosen logits.

Understand the idea

A mixture-of-experts layer has many expert networks but runs only a few for each token. A router gives each expert a score, keeps the top k, and turns just those k scores into mixing weights that add up to 1. Like a hospital front desk sending each patient to the two most suitable doctors. Worked example, 4 experts, k = 2. Token 1 logits [1, 3, 2, 3]: the best two are experts 1 and 3 (tie at 3, lower index first), and softmax over [3, 3] gives [0.5, 0.5]. Token 2 logits [0, 2, 1, 0]: best are experts 1 and 2, softmax over [2, 1] gives [0.731, 0.269]. Expert load counts every chosen slot: expert 0 got 0, expert 1 got 2, expert 2 got 1, expert 3 got 1, so [0, 2, 1, 1].

stable sort on negated logits, take first k, softmax over only those k

Mixtral and other MoE LLMs route tokens this way, and uneven load slows training.

Topics
Efficient architecturesLLM architecture track
Companies1
  • Mistral the topic appears in interview reports Mistral AI Interview Guide 2026: European Frontier Lab, Open-Weight Strategy, MoE, and La Plateforme

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

Similar questions
  • Share keys and values across heads with grouped-query attentionMedium
  • Causal linear attention with a running stateMedium
  • Route tokens with expert capacity and drop the overflowMedium
  • Route tokens with Mixture-of-DepthsMedium
All Efficient architectures problems
Report
487 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
7
8
9
10
import numpy as np
​
def moe_route(logits, k=2):
# your code here
pass
​
def expert_load(idx, n_experts):
# your code here
pass
​
np.asarray(moe_route(L)[0]).tolist()

This case uses generated data, so it runs as written. Press + to add your own case.

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