Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Encrypt String With Shift

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

Your question is Encrypt String With 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

Hitachi Vantara tools may need lightweight text transformations for demonstrations or test fixtures. Implement a Caesar-shift function that encrypts an ASCII string by moving each alphabetic character forward by a given integer shift.

Requirements

  1. Shift lowercase letters within a through z and uppercase letters within A through Z.
  2. Wrap around when a shift passes z or Z.
  3. Support positive, zero, and negative shift values. A negative shift moves characters backward.
  4. Preserve the case of every letter.
  5. Leave digits, whitespace, punctuation, and other non-ASCII characters unchanged.

Formal Specification

  • Input: text, a string, and shift, an integer.
  • Output: A new string containing the transformed characters.
  • The function must not modify the original string.
  • This is a reversible Caesar transformation, not production-grade encryption.

Constraints

  • 0 <= len(text) <= 10^5
  • -10^9 <= shift <= 10^9
  • Only ASCII letters are shifted
  • Digits, whitespace, punctuation, and other Unicode characters remain unchanged

Function Signature

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