Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implement Python Caesar Cipher

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

Your question is Implement Python Caesar Cipher. 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

Canonical tooling may need to transform short diagnostic or configuration strings before transmitting them through an Ubuntu service. Implement a Caesar cipher that shifts every ASCII letter by a specified amount while leaving digits, spaces, punctuation, and other characters unchanged.

Formal specification

Implement caesar_encrypt(text, shift), where text is a string and shift is an integer. Lowercase letters must remain lowercase, uppercase letters must remain uppercase, and shifts must wrap around the alphabet. Positive shifts encrypt forward, while negative shifts move backward. Return the encrypted string. Do not modify the input string.

The function should support shifts larger than 26 by treating them modulo 26. For this problem, only characters in A-Z and a-z are shifted. All other characters are copied exactly.

Constraints

  • 0 <= len(text) <= 10^5
  • -10^9 <= shift <= 10^9
  • Only ASCII letters in A-Z and a-z are transformed
  • All other characters are preserved exactly
  • The output length equals the input length

Function Signature

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