Simple Cipher Amazon OA 2022

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.


  • encryped: a string
  • k: an integer


the decrypted string


Example 1:


1encryped = VTAOG

2k = 2

Output: TRYME


Looking back 2 from ‘V’ returns ‘T’, from ‘T’ returns ‘R’ and so on. The decrypted string is ‘TRYME’.

Simple Cipher Amazon OA Solution
Simple Cipher Amazon OA 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="";
    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);
    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() {
string s="TSNFA";
int k=10;
for(int i=0;i<s.size();i++){
    char it=s[i];
for(char it:s){
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'
                _encrypted[i] = (char)(x-k);
            //if ascii code of kth previous element if less than that of A add 26 to it
                _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

Related: Amazon OA Online Assessment 2021 Questions and Answers