Prepin
Log in

Decode Message

Approach / Explanation

In the task titled "Decrypt Message," you are usually presented with an encrypted string, and your objective is to devise an algorithm to decrypt it. The question may specify a particular decryption method, such as shifting each letter in the message by a certain number of positions in the alphabet, commonly referred to as a Caesar cipher. Alternatively, it could involve replacing each letter with a different specific letter, which would require a decryption key. The key concepts and skills that may be assessed in this question include: Your ability to understand and apply string manipulation techniques. Your knowledge of character ASCII values (if that is applicable to the encryption method). Your proficiency in accurately implementing the specified decryption algorithm. It's important to clarify the original method of encryption, as this is crucial for successful decryption. Additionally, inquire with the interviewer whether you need to consider uppercase and lowercase letters, or if the input may include special characters or numbers. Also, check if the programming language you are using has specific functions for message decryption.

Suggested Answer

If the characters in the string are shifted by one position in the English alphabet, the following Python code can accomplish this: def decrypt_message ( message ) : decrypted_message = "" for character in message : if character . isalpha ( ) : shift = 3 if character . lower ( ) == "x" else 1 if character . lower ( ) == "y" else 2 if character . islower ( ) else - 1 decrypted_message += chr ( ( ord ( character ) - shift - 97 ) % 26 + 97 ) else : decrypted_message += character return decrypted_message This Python script employs a for loop to go through each character in the message. If the character is a letter, it is shifted by either 1 or 3 positions based on the letter. The ‘ord’ function is used to get the ASCII numeric value of the character, while ‘chr’ converts this value back into a character.

Alternative Answer

Imagine a situation in which a decryption key is represented as a dictionary, with the encrypted characters as keys and their corresponding decrypted characters as values. A Python function that utilizes this decryption key to decode a message could be structured as follows: def decrypt_message ( message , decryption_key ) : return "" . join ( decryption_key . get ( character , character ) for character in message ) This code snippet employs a Python generator to loop through each character in the message. For every character, it utilizes the dictionary.get() method to retrieve the corresponding decrypted character from the decryption key. If a character does not have a matching decrypted counterpart (for instance, if it is a space or punctuation that wasn't encrypted), the original character is returned as is. This process constructs a new string that results in the decrypted message.

Question Details

Difficulty & Category

Medium
algorithms

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

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