Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Build Palindrome Generator

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

Your question is Build Palindrome Generator. 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

Swiggy QA systems sometimes generate deterministic synthetic strings for validating input handling. Implement a function that constructs the lexicographically smallest palindrome under length and character-category constraints.

The function supports two modes:

  1. Length-only mode: If all category counts are zero, return a palindrome of exactly length lowercase a characters.
  2. Category-count mode: Return a palindrome of length length containing exactly letter_count lowercase letters, digit_count digits, and special_count characters. The allowed categories are lowercase English letters, decimal digits, and the special-character set !@#$%^&*.

Within each category, use the smallest possible character: a for letters, 0 for digits, and ! for special characters. Compare complete results using ASCII lexicographic order. Return an empty string if the requested construction is impossible. A palindrome is possible only when at most one category count is odd, and the three category counts must sum to length in category-count mode.

Formal Specification

Given integers length, letter_count, digit_count, and special_count, return a string of type str. The result must be a palindrome, have exactly length characters, and satisfy the requested category counts.

Constraints

  • 1 <= length <= 100000
  • 0 <= letter_count, digit_count, special_count <= length
  • In category-count mode, letter_count + digit_count + special_count must equal length
  • Only lowercase letters, decimal digits, and characters from !@#$%^&* are allowed

Function Signature

def generate_palindrome(length, letter_count, digit_count, special_count):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output