Cipher: Implement a Caesar and Vigenere Cipher Encoder and Decoder
Interview Experience
Problem
Implement encode and decode for two ciphers:
Part 1 - Caesar Cipher: Shift each letter in text by shift positions in the alphabet (wrap around). Non-letter characters are unchanged. Decoding shifts in the opposite direction.
python
def caesar_encode(text: str, shift: int) -> str: ...
def caesar_decode(text: str, shift: int) -> str: ...
Example:
caesar_encode("Hello, World!", 3) -> "Khoor, Zruog!"
caesar_decode("Khoor, Zruog!", 3) -> "Hello, World!"
Part 2 - Vigenere Cipher: Use a repeating keyword. Each letter is shifted by the corresponding keyword letter's position (a=0, b=1, ...).
python
def vigenere_encode(text: str, key: str) -> str: ...
def vigenere_decode(text: str, key: str) -> str: ...
Example:
vigenere_encode("ATTACKATDAWN", "LEMON")
A+L=L, T+E=X, T+M=F, A+O=O, C+N=P, K+L=V, ...
-> "LXFOPVEFRNHR"
Follow-ups
- Why is the Caesar cipher trivially broken? How many keys exist?
- How would you perform a frequency-analysis attack on a Caesar-encoded message?
- What is the index of coincidence, and how does it help determine the key length of a Vigenere cipher?
- How does your implementation handle mixed-case text and Unicode input?
Full Details
Problem
Implement encode and decode for two ciphers:
Part 1 - Caesar Cipher: Shift each letter in text by shift positions in the alphabet (wrap around). Non-letter characters are unchanged. Decoding shifts in the opposite direction.
python
def caesar_encode(text: str, shift: int) -> str: ...
def caesar_decode(text: str, shift: int) -> str: ...
Example:
caesar_encode("Hello, World!", 3) -> "Khoor, Zruog!"
caesar_decode("Khoor, Zruog!", 3) -> "Hello, World!"
Part 2 - Vigenere Cipher: Use a repeating keyword. Each letter is shifted by the corresponding keyword letter's position (a=0, b=1, ...).
python
def vigenere_encode(text: str, key: str) -> str: ...
def vigenere_decode(text: str, key: str) -> str: ...
Example:
vigenere_encode("ATTACKATDAWN", "LEMON")
A+L=L, T+E=X, T+M=F, A+O=O, C+N=P, K+L=V, ...
-> "LXFOPVEFRNHR"
Follow-ups
- Why is the Caesar cipher trivially broken? How many keys exist?
- How would you perform a frequency-analysis attack on a Caesar-encoded message?
- What is the index of coincidence, and how does it help determine the key length of a Vigenere cipher?
- How does your implementation handle mixed-case text and Unicode input?
About This Question
This is a candidate experience report from a karat interview.
It covers the following topics: Coding .