Simple Cipher Amazon OA 2023

Simple Cipher Amazon OA 2023 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 string
  • k: 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’.

Simple Cipher Amazon OA Solution
Simple Cipher Amazon OA Solution

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 OA 2023 Questions with Solution

  1. Shopping Patterns Solution Amazon OA 2023
  2. Reorder Data in Log Files Solution Amazon OA 2023
  3. Top K Frequent Words Solution Amazon OA 2023
  4. Trees Height Solution Amazon OA SDE 2023
  5. Counting Binary Substrings Amazon OA 2023
  6. Grid Connections Amazon OA 2023
  7. Shipment Imbalance Amazon OA 2023
  8. Max Profit Amazon OA 2023
  9. Find Lowest Price Amazon OA 2023
  10. Decode String Frequency Amazon OA 2023
  11. Valid Discount Coupons Amazon OA 2023 Solution
  12. Count Maximum Teams Amazon OA 2023
  13. Minimum Coin Flips Amazon OA 2023
  14. Max Average Stock Price Amazon OA 2023 Solution
  15. Robot Bounded In Circle Amazon OA 2023
  16. Shopping Options Amazon OA 2023 Solution
  17. Fill The Truck Maximum Units on a Truck Amazon OA Solution
  18. Maximize Score After N Operations Number Game Solution Amazon OA 2023
  19. Slowest Key Amazon OA 2023 Solution
  20. Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
  21. Split String Into Unique Primes Amazon OA 2023 Solution
  22. Storage Optimization Amazon OA 2023 Solution
  23. Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
  24. Autoscale Policy Utilization Check Amazon OA 2023
  25. Optimal Utilization Solution Amazon OA 2023
  26. Merge Two Sorted Lists Solution Amazon OA 2023
  27. Two Sum Unique Pairs Solution Amazon OA 2023
  28. Amazon Music Pairs Amazon OA 2023 Solution
  29. Class Grouping Amazon OA 2023 Solution
  30. Find Max products Amazon OA 2023 Solution
  31. Get encrypted number Amazon OA 2023 Solution
  32. Find Total Imbalance Amazon OA 2023 Solution
  33. Find Total Power Amazon OA 2023 Solution