Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

ROT Cipher Shift

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is ROT Cipher Shift. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Grubhub wants a lightweight way to obfuscate short internal delivery messages. Implement a Caesar cipher that shifts every English letter by k positions while preserving letter case and leaving digits, spaces, and punctuation unchanged.

Formal Specification

Write encrypt_message(message, k), where message is a string containing printable ASCII characters and k is an integer. Return a new string with each uppercase letter shifted cyclically within A-Z and each lowercase letter shifted cyclically within a-z. The shift must wrap around, and negative values of k must shift letters backward. Non-letter characters must remain exactly unchanged.

For example, with k = 3, A becomes D, x becomes a, and ! remains !.

Constraints

  • 0 <= len(message) <= 10^5
  • -10^9 <= k <= 10^9
  • message contains printable ASCII characters
  • Uppercase and lowercase letters are shifted within their own alphabets
  • Non-letter characters remain unchanged

Function Signature

def encrypt_message(message, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output