Palindromic Strings
Updated
Problem statement
A string is a palindrome if the reverse of the string is the same as the original string. An analyst at HackerRank is tasked to analyze an array of n strings that consist of lowercase English characters.
In one operation, the analyst chooses 4 integers, namely x, y, i, j such that 1 ≤ x, y ≤ n, 1 ≤ i ≤ length(arr[x]), 1 ≤ j ≤ length(arr[y]) and then swaps arr[x][i] and arr[y][j] using 1-based indexing.
The analyst wishes to determine the maximum number of palindromic strings that can be obtained by performing the operation any number of times, possibly 0.
Function
countPalindromes(arr: String[]) → int
Complete the function countPalindromes in the editor below.
countPalindromes has the following parameter:
string arr[n]: the strings to consider
Returns int: the maximum number of palindromic strings possible
Examples
Example 1
arr = ["pass", "sas", "asps", "df"]return = 3An optimal solution produces 3 palindromes.
- Select
x = 1,y = 3,i = 3,j = 1, swaparr[1][3],arr[3][1]. Then,arr= ["paas", "sas", "ssps", "df"] - Select
x = 1,y = 3,i = 4,j = 3, swaparr[1][4],arr[3][3].arr= ["paap", "sas", "ssss", "df"]
Return 3, the number of palindromic strings.
More NVIDIA interview questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Query a Header-Encoded MatrixOA · Seen Oct 2026
- Maximum ScoreOA · Seen Oct 2026
- Address on Same PageOA · Seen Oct 2026
- Minimize Task Resource ConsumptionOA · Seen Oct 2026
- Minimum Project Bid CostOA · Seen Oct 2026
- Count Ideal Numbers in a RangeOA · Seen Oct 2026
Similar questions at other companies
- Lexicographically Largest IdentifierAmazon · OA
- Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
- Longest Repeated Letter Block with Adjacent SwapsIntuit · OAIntuit interview questions
- Binary CircuitAkuna Capital · OAAkuna Capital interview questions
- Minimize Expression with ParenthesesPinterest · OAPinterest interview questions
- Word Wrap with Even JustificationSentry · OA · PHONE SCREENSentry interview questions
- Get Patch SequenceMorgan Stanley · OAMorgan Stanley interview questions
- Max Number of OperationsSalesforce · OASalesforce interview questions
- Viral Content BalancerTikTok · OATikTok interview questions
- Good Binary StringsBNP Paribas · OA