Prepin
Log in

M

Save a collection of numbers as one single numerical value.

Approach / Explanation

The interviewer is seeking a method to represent a list of numbers as a single number. The interpretation may vary based on the context, so it's important to clarify their intent. Are they asking you to generate a single number that encompasses all the numbers in the list? This could be achieved by converting the numbers to strings, concatenating them, and then converting the final result back into a number. Alternatively, does the interviewer want you to implement an encoding method to encapsulate the list's information within a single number? If the numbers in the list are sufficiently small, this could be accomplished using bitwise operations. Below, we will examine both options.

Suggested Answer

If the interviewer wishes to consolidate all the numbers into a single concatenated number, this can be accomplished in Python as demonstrated below: def list_to_number ( lst ) : str_list = [ str ( i ) for i in lst ] str_num = "" . join ( str_list ) return int ( str_num ) lst = [ 1 , 2 , 3 , 4 , 5 ] print ( list_to_number ( lst ) ) # Outputs: 12345 In this example, each number from the list is transformed into a string, and these strings are then combined to create a single string. This final string is subsequently converted back into a number using Python's built-in int() function.

Alternative Answer

If the interviewer requests that you consolidate the information from a list into a single number, you might want to consider employing a method such as the one outlined below: def list_to_number ( lst , max_num_bits ) : result = 0 for num in lst : result = ( result << max_num_bits ) | num return result lst = [ 1 , 2 , 3 ] max_number_in_list = max ( lst ) max_number_bits = max_number_in_list . bit_length ( ) print ( list_to_number ( lst , max_number_bits ) ) # Outputs: 29 This example demonstrates the use of bitwise operations to encode an entire list of numbers into a single integer. By shifting the result left by max_num_bits and combining it with the current number using the bitwise OR operator, we effectively store the binary representations of all numbers in result . The bit_length() function is utilized to determine the number of bits needed to represent the largest number in the list, allowing for the correct shifting of the result .

Question Details

Difficulty & Category

Medium
data structures

Companies

Atlassian
Microsoft

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.