Problem · String

Encode or Decode Message

Learn this problem
EasyGoldman Sachs logoGoldman SachsOA

Problem statement

Encodes or decodes a message using a shared numeric key.

Input

  • Operation:
    • 1 = Encode
    • 2 = Decode
  • Message (string)
  • Key (positive integer)

Encoding (Operation 1)

Duplicates each character based on the corresponding digit in the key.

Example:

Message: Open, Key: 123 → Output: Oppeen

Decoding (Operation 2)

Compresses repeated characters based on digits in the key.

Example:

Message: Oppeen, Key: 123 → Output: Open

Rules

  • Apply digits in key cyclically or until message ends.
  • Remaining characters (if key ends first) are left unchanged.
  • Return -1 for invalid input or mismatched patterns during decoding.
  • Function

    encodeOrDecode(operation: int, message: String, key: int) → String

    Examples

    Example 1

    operation = 1message = "Open"key = 123return = "Oppeen"
    ~~

    Example 2

    operation = 2message = "Oppeen"key = 123return = "Open"
    ~~~

    More Goldman Sachs problems

    drafts saved locally
    public String encodeOrDecode(int operation, String message, int key) {
      // write your code here
    }
    
    operation1
    message"Open"
    key123
    expected"Oppeen"
    checking account