Encode or Decode Message

Problem Overview

  • Encode or decode a message using a shared numeric key; apply key digits cyclically.
  • Inputs/outputs: operation (1 encode, 2 decode), message string, positive integer key; return string or -1 for invalid input or mismatched patterns.
  • Constraints: if key ends first, remaining characters stay unchanged when encoding; decoding must validate repeats; nonconformance returns -1.
  • Context: String transformation for a coding interview; a classic interview question problem.

Example

Unlock to view complete problem details

and practice with sample input/output

Was this article helpful?

View Test Cases & Run Code requires membership

Input Variables
Execution Result: