← Walmart Interview Insights

Walmart·Software Engineer·Online Assessment (OA)·Junior

JuniorRejected
May 2026Remote

Summary

Took an OA with a string manipulation problem that combined palindrome and k-periodicity constraints. Had the right idea with Union-Find but fumbled a one-line bug in the join logic and got zero points on it. Still stings.

Questions Asked (1)

Q1

Given a string of length n and an integer k, find the minimum number of character changes needed to make the string both a palindrome and k-periodic simultaneously.

Algorithms & Data Structures
Author's notes

My approach was Union-Find: group indices that must share the same character (from both the palindrome constraint and the periodicity constraint), then for each group just count how many characters aren't the majority character.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the constraints as a graph where each position must equal its mirror and positions congruent modulo k must be equal. Use union-find to group positions that must share the same character, then for each group choose the character that minimizes changes. Sum the minimum changes across all groups to get the answer.

Pro tip: Mention that the solution runs in O(n α(n)) time and O(n) space, and that it handles edge cases like k=1 or k≥n gracefully. This shows you consider efficiency and robustness.

1. Understand the constraints

Clarify that the string must be a palindrome (s[i] = s[n-1-i]) and k-periodic (s[i] = s[i+k] for all valid i). These are equality constraints on positions.

2. Build a graph of constraints

Create a graph where each position is a node, and add edges between positions that must be equal: (i, n-1-i) for palindrome and (i, i+k) for periodicity.

3. Find connected components

Use union-find (or BFS/DFS) to group positions that must all have the same character. Each component represents a set of positions that must be identical.

4. Compute minimum changes per component

For each component, count the frequency of each character. The minimum changes for that component is the total size minus the maximum frequency. Sum these over all components.

5. Return the total minimum changes

The sum from step 4 is the minimum number of character changes needed to satisfy both constraints simultaneously.

Key Points to Mention

  • Modeling the problem as a graph of equality constraints
  • Using union-find (disjoint set union) for efficient grouping
  • Time complexity: O(n α(n)) with union-find, which is nearly linear
  • Space complexity: O(n) for the union-find structure and frequency counts
  • Handling edge cases: k=1 (all characters must be same), k ≥ n (only palindrome constraint)
  • The greedy choice of most frequent character per component is optimal

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.