KatInterview

Decode a Mixed-Radix Positional Number

Math & Bit ManipulationAuto-graded · 17 test cases
MediumAuto-gradedMath & Bit Manipulationmathbase conversionnumber theory

Last updated

Tests the ability to convert a number represented in a variable base-per-digit (mixed-radix) system back to its standard base-10 integer value.

Decode a Mixed-Radix Positional Number

A mixed-radix number system generalizes ordinary positional notation by allowing each digit position to have its own base (radix) instead of a single shared base.

Formally, given a sequence of bases b[0], b[1], ..., b[n-1] and a corresponding sequence of digits d[0], d[1], ..., d[n-1], the decimal value is computed as:

value = d[0] * (b[1] * b[2] * ... * b[n-1])
      + d[1] * (b[2] * b[3] * ... * b[n-1])
      + ...
      + d[n-2] * b[n-1]
      + d[n-1]

In other words, position i (0-indexed from the left / most-significant side) has a place value equal to the product of all bases that come after it: b[i+1] * b[i+2] * ... * b[n-1]. The last position always has place value 1.

This is identical to ordinary base conversion when all bases are equal (e.g., all 10 gives decimal, all 2 gives binary), but here each b[i] may differ.

Your task: Given bases and digits, return the non-negative integer they represent in base 10.

Validity guarantee: For every position i, 0 <= digits[i] < bases[i]. You do not need to validate inputs.

Constraints

  • 1 <= len(bases) == len(digits) <= 2000
  • 2 <= bases[i] <= 10^4 for all i
  • 0 <= digits[i] < bases[i] for all i
  • The answer may be very large; return the exact Python integer (no modular reduction).
  • If len(bases) == 1, there is exactly one digit; its place value is 1, so the answer equals digits[0].

Examples

Example 1

Input: decode_mixed_radix([10, 10, 10], [3, 7, 5])

Output: 375

Example 2

Input: decode_mixed_radix([2, 2, 2, 2], [1, 0, 1, 1])

Output: 11

Example 3

Input: decode_mixed_radix([24, 60, 60], [1, 30, 45])

Output: 5445

Hints

Hint 1

Process digits from right to left (least-significant to most-significant), keeping a running place value that you multiply by the next base as you move left.

Hint 2

Alternatively, use Horner's-method-style evaluation left to right: accumulate `result = result * bases[i] + digits[i]`, which avoids storing the full place value.

Hint 3

Edge cases to watch: a single-element list (answer is just digits[0]), and all-zero digits (answer is 0 regardless of bases).