Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Even-Length String Logic

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

Your question is Even-Length String Logic. 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

Insurity policy workflows may need a deterministic string transformation before displaying or processing values in ClaimsXPress. Implement a function that examines the length of an input string and returns the original string when its length is even, or the characters in reverse order when its length is odd.

The transformation must preserve every character, including spaces, punctuation, digits, and letter case. The parity decision must use Python's standard string length, so each Unicode code point contributes one to the length.

Formal Specification

  • Input: s, a non-null Python str.
  • Output: A Python str containing either the original value or its reversal.
  • If len(s) % 2 == 0, return s unchanged.
  • Otherwise, return s reversed.
  • Do not modify the input string. Python strings are immutable, so returning a new reversed string is acceptable.

Constraints

  • 0 <= len(s) <= 10^5
  • s is a non-null Python string
  • s may contain Unicode characters, whitespace, punctuation, and digits
  • The input string must not be modified

Function Signature

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