Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Build Palindrome Generator
00:00
5 left

Build Palindrome Generator

HardPython

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):
Interviewer

Your question is Build Palindrome Generator. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.