← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Senior

Senior
Jul 2026

Summary

Google SWE interview with a pretty gnarly combinatorics/string problem about splitting a circular necklace. The follow-up pushed it into three gem types which is where things got spicy. No fluff, just pure algorithm design from start to finish.

Questions Asked (2)

Q1

You have a circular necklace represented as a string over {'a','b'}. You can cut it at most twice to get up to three contiguous pieces. Can you assign those pieces to two people so each person gets the same count of 'a' beads and the same count of 'b' beads? If yes, return valid cut indices and the assignment; otherwise return 'impossible'. You also need to state necessary feasibility conditions, give an O(n) algorithm, and analyze correctness and complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The parity stuff tripped me up first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem and establish necessary conditions: total counts of 'a' and 'b' must be even. Then, reduce the problem to finding a contiguous segment (or two segments) with exactly half the total counts, using prefix sums and a hash map for O(n) time. Finally, describe how to map the found segment(s) to cuts and assignment, and prove correctness and complexity.

Pro tip: Emphasize that the problem is equivalent to partitioning the circular string into two parts with equal counts, which can be solved by checking for a contiguous subarray with half the total counts. Mention that if the total counts are not even, it's immediately impossible, and that the O(n) solution uses a hash map to store prefix sum differences.

1. Clarify and Define

Restate the problem: we can cut the circular string at most twice to get up to three contiguous pieces, and assign pieces to two people so each gets equal counts of 'a' and 'b'. Define necessary conditions: total count of 'a' and 'b' must each be even.

2. Reduce to Subarray Search

Observe that assigning pieces to two people is equivalent to partitioning the circle into two sets of pieces with equal total counts. This can be achieved if there exists a contiguous segment (possibly wrapping around) that contains exactly half of each character. Thus, the problem reduces to finding a contiguous subarray with target counts (total_a/2, total_b/2).

3. Design O(n) Algorithm

Use prefix sums on the doubled string to handle circularity. Maintain a hash map from (count_a - target_a, count_b - target_b) to the earliest index. Iterate through the doubled string, updating counts and checking if the difference has been seen before. If found, the segment between the stored index and current index is a valid piece. Then determine cuts and assignment.

4. Handle Edge Cases and Output

If the segment is the entire string, no cuts are needed (0 cuts). If the segment is a prefix or suffix, one cut suffices. Otherwise, two cuts are needed. Return the cut indices (0-based) and assignment (e.g., piece1 to person1, rest to person2). If no segment found, return 'impossible'.

5. Prove Correctness and Complexity

Argue that any valid partition corresponds to a contiguous segment with half counts, and vice versa. The algorithm checks all possible segments in O(n) time using the hash map, and O(n) space for the map and prefix sums.

Key Points to Mention

  • Necessary condition: total counts of 'a' and 'b' must be even.
  • Reduction to finding a contiguous subarray with exactly half the total counts of each character.
  • Use of prefix sums and hash map to achieve O(n) time by storing differences (count_a - target_a, count_b - target_b).
  • Handling circularity by doubling the string or using modulo arithmetic.
  • Mapping the found segment to cuts: 0 cuts if entire string, 1 cut if prefix/suffix, 2 cuts otherwise.
  • Correctness proof: bijection between valid assignments and contiguous segments with half counts.

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

Q2

Extend the necklace problem to three gem types {'a','b','c'} where you may make at most three cuts to produce up to four pieces, still splitting between two people so each receives equal counts of every gem type. Give the generalized algorithm, state all required conditions, and discuss how the approach scales as the number of gem types grows.

Algorithms & Data StructuresTechnical Trade-offsAdaptability & Ambiguity
Author's notes

I did not see this coming.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by restating the problem and clarifying assumptions, then present the generalized algorithm using a discrete approach with prefix sums and search for cut points. Discuss necessary conditions for feasibility and analyze scalability in terms of gem types and cuts.

Pro tip: Emphasize that the problem reduces to finding a contiguous subarray with exactly half the total of each gem type, and that with k cuts you can create up to k+1 pieces; for three gem types, three cuts suffice to isolate such a subarray. Mention that the algorithm generalizes but becomes computationally intensive as gem types increase.

1. Clarify the problem and assumptions

Restate the problem: given a necklace with gems of types a, b, c, make at most 3 cuts to split into up to 4 pieces, then partition pieces between two people so each gets equal counts of each gem type. Assume the necklace is linear (or circular) and cuts are between gems.

2. Identify necessary conditions

For a fair split, the total count of each gem type must be even. Also, the total number of gems must be even. If these conditions are not met, no solution exists.

3. Present the generalized algorithm

Use prefix sums for each gem type to represent the necklace as a sequence of triples. The goal is to find a contiguous segment (possibly wrapping around if circular) that contains exactly half of each gem type. With up to 3 cuts, we can create up to 4 pieces; by selecting a contiguous segment as one person's share, the remaining pieces form the other's share. The algorithm searches for such a segment by iterating over possible start and end points, using the prefix sums to check if the segment has the target counts. For three gem types, a linear scan with two pointers or a hash map of prefix sum differences can find a valid segment in O(n) time.

4. Discuss scalability and trade-offs

As the number of gem types k increases, the state space for prefix sums grows exponentially (k-dimensional). The problem becomes equivalent to finding a subset of pieces with target sums, which is NP-hard for large k. For small k (like 3), efficient algorithms exist; for larger k, approximation or heuristic methods may be needed. Also, the number of cuts allowed affects the number of pieces and thus the complexity.

Key Points to Mention

  • Necessary condition: total count of each gem type must be even.
  • Reduction to finding a contiguous subarray with exactly half of each gem type.
  • Use of prefix sums to efficiently compute gem counts in any segment.
  • With k cuts, you can create up to k+1 pieces; for 3 gem types, 3 cuts suffice to isolate a valid segment.
  • Algorithm complexity: O(n) for fixed number of gem types using hashing or two pointers.
  • Scalability: as gem types grow, problem becomes NP-hard; discuss trade-offs and possible approximations.

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