Encode or Decode Message
Updated
Problem statement
Encode or decode a message using the decimal digits of a positive integer key.
Encoding
When operation == 1, process message characters from left to right. Repeat character i according to key digit i. If the key ends first, copy the remaining message characters once. For example, encoding Open with key 123 produces Oppeeen.
Decoding
When operation == 2, consume one equal-character group for each key digit and append one copy of that character. If the key ends first, copy the remaining suffix unchanged. For example, decoding Oppeeen with key 123 produces Open.
Return -1 when the operation is unsupported, a used key digit is zero, or a required decoding group is missing or contains different characters.
Function
encodeOrDecode(operation: int, message: String, key: int) → StringExamples
Example 1
operation = 1message = "Open"key = 123return = "Oppeeen"Repeat O once, p twice, and e three times. The key then ends, so n is copied once, producing Oppeeen.
Example 2
operation = 2message = "Oppeeen"key = 123return = "Open"Consume one O, two p characters, and three e characters. The remaining n is copied unchanged, producing Open.
Constraints
operationis an integer.messageis a string.keyis a positive 32-bit integer.
More Goldman Sachs interview questions
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Hash MapOA · PHONE SCREEN · Seen Nov 2026
- House Robber IIIPHONE SCREEN · Seen Nov 2026
- Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
- Maximum Ones After K OperationsOA · Seen Nov 2026
- Palindrome CounterOA · Seen Oct 2026
- Longest Palindromic SubsequenceOA · Seen Oct 2026
- Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Theme Park Shuttle Wait TimeAirbnb · OAAirbnb interview questions
- Branch with the Most FilesCapital One · OACapital One interview questions
- Robot Stops Left or Right (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- REST API: Highest International StudentsRippling · OARippling interview questions
- Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
- Filter Isolated Interior Signal BitsSEDEMAC · OA
- Scale Ingredient QuantitiesUpstart · OAUpstart interview questions
- Telephone Keypad String EncodingEpic · OAEpic interview questions
- Count Invalid Log GroupsIBM · OAIBM interview questions