Skip to main content

Find the Key of the Numbers - Solution & Explanation

EasyMath18 min readAsked at: Google
Practice this problem

Problem Statement

You are given three positive integers num1, num2, and num3.

The key of num1, num2, and num3 is defined as a four-digit number such that:

  • Initially, if any number has less than four digits, it is padded with leading zeros.
  • The ith digit (1 <= i <= 4) of the key is generated by taking the smallest digit among the ith digits of num1, num2, and num3.

Return the key of the three numbers without leading zeros (if any).

 

Example 1:

Input: num1 = 1, num2 = 10, num3 = 1000

Output: 0

Explanation:

On padding, num1 becomes "0001", num2 becomes "0010", and num3 remains "1000".

  • The 1st digit of the key is min(0, 0, 1).
  • The 2nd digit of the key is min(0, 0, 0).
  • The 3rd digit of the key is min(0, 1, 0).
  • The 4th digit of the key is min(1, 0, 0).

Hence, the key is "0000", i.e. 0.

Example 2:

Input: num1 = 987, num2 = 879, num3 = 798

Output: 777

Example 3:

Input: num1 = 1, num2 = 2, num3 = 3

Output: 1

 

Constraints:

  • 1 <= num1, num2, num3 <= 9999

Approach Overview

Problem Overview: You receive three integers. Treat each as a 4-digit number (pad with leading zeros if necessary). For every digit position (thousands, hundreds, tens, ones), pick the smallest digit among the three numbers. Combine those minimum digits to form the final 4-digit key.

Approach 1: Basic Approach with String Manipulation (O(d) time, O(d) space)

This method converts each number into a string and ensures it has exactly four characters using leading zeros. You iterate from index 0 to 3, compare the digits from the three strings at the same position, and select the minimum. Append the smallest digit to a result string and convert the final string back to an integer.

The key insight is that digit positions must align. Converting to strings makes this trivial because indexing directly represents each place value. This approach is straightforward to implement and easy to debug, which is why many developers prefer it in interviews when clarity matters.

Approach 2: Bitwise Operations and Mathematical Approach (O(d) time, O(1) space)

This version avoids strings entirely and works directly with digits using arithmetic operations. Repeatedly extract the last digit of each number using % 10. Compute the minimum among those digits, then build the result number using positional multiplication. After processing a digit, remove it from each number using integer division / 10.

You repeat this process four times (since the key always has four digits). The result is constructed by multiplying the chosen minimum digit with the correct place value (1, 10, 100, 1000). This approach uses constant memory and relies purely on numeric operations from math. It is typically faster in low-level implementations because it avoids string allocation.

Even though the platform labels this as a bitwise-style approach, the core idea is digit extraction through arithmetic. It demonstrates strong understanding of number manipulation, which often appears in math and bit manipulation style problems.

Recommended for interviews: The mathematical digit extraction approach is usually preferred. It uses constant extra space and shows that you understand how numbers are represented digit-by-digit. The string method still demonstrates correct reasoning and is perfectly acceptable for an easy problem, especially if the focus is readability rather than micro-optimizations. Understanding both approaches helps when similar digit-processing problems appear in string or numeric manipulation tasks.

Approach 1: Basic Approach with String Manipulation

In this approach, the main idea is to convert each of the numbers into a four-digit string, padded with leading zeros if needed. Then, by iterating over corresponding digits of each number, we can compute the result by taking the minimum digit at each position. Finally, any leading zeros from the result should be trimmed to adhere to the output requirements.

The solution uses the C standard library function sprintf to convert integers into zero-padded four-digit strings. It then computes the minimum digit for each of the four positions, constructs a new key as a string, and trims leading zeros before returning the result. Using static for the returned string ensures persistence outside the function.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) because we are iterating through fixed 4 digits.
Space Complexity: O(1) as we use a constant amount of extra space.

Try this approach in the editor →

Approach 2: Bitwise Operations and Mathematical Approach

This approach is based on handling the digits through bitwise operations and direct mathematical calculations without converting numbers to strings. This helps in environments where low-level operations are preferred. The idea is to extract individual digits from each number, compare them, and construct the final key purely with arithmetic operations.

This solution directly extracts each digit using division and modulo operations without converting the numbers to strings. By iterating over predefined factors, each digit is directly computed, minimizing the necessity for string manipulations and thus improving efficiency and clarity.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1).
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Simulation

We can directly simulate this process by defining a variable ans to store the answer and a variable k to represent the current digit place, where k = 1 represents the units place, k = 10 represents the tens place, and so on.

Starting from the units place, for each digit place, we calculate the current digit of num1, num2, and num3, take the minimum of the three, and then add this minimum value multiplied by k to the answer. Then, multiply k by 10 and continue to the next digit place.

Finally, return the answer.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Basic Approach with String Manipulation

Time Complexity: O(1) because we are iterating through fixed 4 digits.
Space Complexity: O(1) as we use a constant amount of extra space.

Bitwise Operations and Mathematical Approach

Time Complexity: O(1).
Space Complexity: O(1).

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String ManipulationO(d)O(d)Best for readability and quick implementation using string indexing
Mathematical Digit ExtractionO(d)O(1)Preferred when optimizing memory and working directly with digits

Video Solution

3270. Find the Key of the Numbers (Leetcode Easy) • Programming Live with Larry • 291 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Find the Key of the Numbers easy or hard?
Find the Key of the Numbers is categorized as an Easy problem. It mainly tests understanding of digit positions, simple comparisons, and number manipulation. Most solutions are implemented in a few lines using either string processing or basic math.
Find the Key of the Numbers Python/Java solution
In Python or Java, convert numbers to 4-digit strings and compare digits position by position, or repeatedly extract digits using % 10 and / 10. The minimum digit from the three numbers at each place forms the final key. Both implementations run in constant time due to the fixed number of digits.
How to solve Find the Key of the Numbers in O(n)?
Treat each number as a 4-digit value and process one digit position at a time. Extract digits either by string indexing or by using modulo and division operations. Compute the minimum digit among the three numbers at each position and append it to the result key. The algorithm runs in O(d) time with constant space when using arithmetic operations.
What is the best approach for Find the Key of the Numbers?
The mathematical digit extraction approach is typically considered the best. It processes each digit using modulo (% 10) and division (/ 10) operations and builds the result numerically. The algorithm runs in O(d) time where d is the number of digits (4 here) and uses O(1) extra space.
Is Find the Key of the Numbers asked at Google/Amazon/Meta?
This problem belongs to the easy math category and is mainly used for practice on digit manipulation and basic algorithmic thinking. Similar digit-processing problems appear in interviews at companies like Amazon and Google, especially in early screening rounds.
What data structure is used in Find the Key of the Numbers?
No complex data structures are required. The solution mainly relies on basic arithmetic operations or simple string manipulation. The core concept comes from math-based digit extraction and positional comparison.
What is the time complexity of Find the Key of the Numbers?
Both common solutions run in O(d) time where d is the number of digits in the numbers. Since the problem always works with 4-digit values, the runtime is effectively constant. Space complexity is O(d) for the string method and O(1) for the mathematical approach.

Ready to solve this problem?

Practice Find the Key of the Numbers with our built-in code editor and test cases.

Practice on FleetCode