Implement Explicit Integer Einsum
Updated
Problem statement
Write your own einsum for integer tensors, without calling a tensor library. An equation has the form term1,term2,...->output. Each input term lists one lowercase label for each axis of its operand. The output labels are all different, and there may be none.
shapes[r] gives the sizes of operand r. values[r] lists that operand's entries in row-major order, so the last axis changes fastest. All uses of one label have the same size, even when a label repeats inside one term.
Try every way of giving each distinct label a coordinate. For each one, read one entry from each operand using that operand's labels, multiply the entries, and add the product to the output cell picked by the output labels. So a label that is not in the output is summed over, a label repeated in one input picks a diagonal, and a label kept in the output lines up across operands.
The output axes follow the label order on the right-hand side. Return the output as a flat row-major array of 64-bit integers. An empty output means a single number, so return an array with one element. For example, ii->i keeps the diagonal of a matrix, and ii-> adds up that diagonal.
Every input is valid and uses only this explicit form. There is no implicit output, no ellipsis, no broadcasting, no scalar operands and no badly formed equation.
Function
einsum(equation: String, shapes: int[][], values: int[][]) → long[]Examples
Example 1
equation = "ij,jk->ik"shapes = [[2,3],[3,2]]values = [[1,2,3,4,5,6],[7,8,9,10,11,12]]return = [58,64,139,154]The label j is summed over. The first output cell is 1*7+2*9+3*11=58, the next is 1*8+2*10+3*12=64, then 4*7+5*9+6*11=139 and 4*8+5*10+6*12=154. The output shape is [2,2], so the answer is [58,64,139,154].
Example 2
equation = "ii->"shapes = [[3,3]]values = [[1,2,3,4,5,6,7,8,9]]return = [15]The repeated i picks the diagonal 1,5,9. An empty output means one number, so the answer is [15].
Example 3
equation = "ab->ba"shapes = [[2,3]]values = [[1,2,3,4,5,6]]return = [1,4,2,5,3,6]The output axes are b then a, so the result is the transpose of the matrix. Reading it row by row gives [1,4,2,5,3,6].
Constraints
1 <= shapes.length = values.length <= 3, matching the number of input terms.- Each input term contains
1through4labels, matching its shape rank; labels are lowercase ASCII letters, and equations contain no spaces. - There are at most
6distinct input labels. Each dimension is from1through8, and all occurrences of a label have equal dimensions. - Each output label occurs in an input term and appears at most once in the output.
- Each flattened input has the product of its shape dimensions and at most
5000entries; the output has at most5000entries. - The product of dimensions of all distinct labels is at most
100000. -100 <= values[r][i] <= 100. Products and sums must use signed64-bit integers.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Basic CalculatorAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
- Expression Add OperatorsPinterest · PHONE SCREENPinterest interview questions
- Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Restricted JSON ParserOld Mission · PHONE SCREEN
- Basic CalculatorNavan · PHONE SCREENNavan interview questions
- Basic CalculatorMotive · PHONE SCREENMotive interview questions
- Basic Calculator with Multiplication and ParenthesesBloomberg LP · PHONE SCREENBloomberg LP interview questions
- JSON ParserPatreon · PHONE SCREENPatreon interview questions
- Straight-Line Word SearchReddit · PHONE SCREENReddit interview questions