Page Contents
Simple Cipher Amazon OA Solution
As part of a Day 1 Challenge, your new team at Amazon has created a basic alphabet-based encryption and has asked members to test the cipher. A simple cipher is built on the alphabet wheel which has uppercase english letters[‘A’-‘Z’] written on it:
Given an encrypted string consisting of english letters[‘A’-‘Z’] only, decrypt the string by replacing each character with the kth character away on the wheel in counter clockwise direction. Counter-clockwise is the opposite direction is which the hands on a clock usually move.
Input
encryped
: a stringk
: an integer
Output
the decrypted string
Examples
Example 1:
Input:
1encryped = VTAOG
2k = 2
Output: TRYME
Explanation:
Looking back 2 from ‘V’ returns ‘T’, from ‘T’ returns ‘R’ and so on. The decrypted string is ‘TRYME’.

SOLUTION
Program: Simple Cipher Amazon OA Solution in C++
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { string s; cin>>s; int k; cin>>k; int n=s.length(); string ans=""; k=k%26; for(int i=0;i<n;i++){ int pos=s[i]-'A'; if(k<=pos) ans+=s[i]-k; else ans+=s[i]+(26-k); } cout<<ans; return 0; }
Program: Simple Cipher Amazon OA Solution in C++
A-Z letters have ascii range 65-90. We can convert char <-> int easily by primitive casting so, we need to take care of edge cases for character to be A or B to replace with Y & Z.
#include <bits/stdc++.h> using namespace std; int main() { //code vectordp={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; string s="TSNFA"; int k=10; for(int i=0;i<s.size();i++){ char it=s[i]; if(it-'A'-k<0){ s[i]=dp[it-'A'-k+26]; } else{ s[i]=dp[it-'A'-k]; } } for(char it:s){ cout<<it; } return 0; }
Program: Simple Cipher Amazon OA Solution in Java
public static String simpleCipher(String encrypted, int k){ char[] _encrypted = encrypted.toCharArray(); for(int i=0; i < encrypted.length(); i++){ char x = _encrypted[i]; // if the previous kth element is greater than 'A' if(x-k>=65){ _encrypted[i] = (char)(x-k); } //if ascii code of kth previous element if less than that of A add 26 to it else{ _encrypted[i] = (char)(x-k+26); } } return new String(_encrypted); }
Program: Simple Cipher Amazon OA Solution in Java
This will give us time complexity = O(input string length) & space complexity ~= O(1) for using in memory replacement * another char[] since we create a new output string
String input = "VTAOG"; int k = 2; int min = 65, max = 90; char[] inputChars = input.toCharArray(); for(int p=0; p < inputChars.length; p++){ int ascii = (int) inputChars[p]; int targetAscii = ascii - k; if(targetAscii < min){ targetAscii = max - (ascii-targetAscii) - 1; //eg. k=3, ascii = 66 ie B, initial targetAscii = 66-3 ie 63, so final targetAscii = 90-(66-63)+1 ie 88 ie X where, '+1' ~= managing array. length-1 for integration target kind of logic } inputChars[p] = (char) targetAscii; //in memory replacement } return new String(inputChars);
Program: Simple Cipher Amazon OA Solution in Python
- The string is made up of uppercase English letters only. That means a total of 26 characters, so if k is > 26 we have to adjust it back to 0..25 by doing modulo.
- Now for each character, I am converting into ASCII and subtracting ASCII of ‘A’ as ‘A’ is our base value and then subtracting k to go k steps backward.
- If the value is < 0 e.g k = 2 and the character is ‘A’ so the value will be -2, but what we want is a value between 0..25, so adding 26 into the value.
- value is between 0..25 and 0 is mapped to ‘A’, 1 is mapped to ‘B’, and so on. So, basically, we have to add ASCII of ‘A’ again into the value.
- Now just convert the ASCII to the character and append it to string.
def simpleCipher(encrypted, k): k %= 26 result = "" for char in encrypted: value = ord(char) - ord('A') - k if value < 0: value += 26 value += ord('A') result += chr(value) return result
Amazon Online Assessment Questions:
- Robot Bounded in Box
- Number Game
- Find All Combination of Numbers Sum to Target / Shopping Options
- Fill the Truck
- Music Pairs
- Slowest key
- Five Star Seller
- Split String Into Unique Primes
- Storage Optimization
- Minimum Difficulty of a Job Schedule
- Autoscale Policy, Utilization Check
- Optimal Utilization
- Merge Two Sorted Lists
- Two Sum Unique Pairs
- Shopping Patterns
- Reorder Data in Log Files
- Top K Frequent Words
- Trees Height
- Counting Binary Substrings Amazon OA Solution
- Grid Connections Amazon OA Solution
- Shipment Imbalance Amazon OA Solution
- Max Profit Amazon OA Solution
- Find Lowest Price Amazon OA Solution
- Simple Cipher Amazon OA Solution
- Decode String Frequency Amazon OA Solution
- Valid Discount Coupons Amazon OA Solution
- Count Maximum Teams Amazon OA Solution
- Minimum Coin Flips Amazon OA Solution