Minimum Boys Next to Girls
Reported by candidates from Guidewire's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Guidewire reported this one in May 2026, and the statement hides a clean greedy problem inside a seating puzzle. You get a string of 'G' and '-', and every girl needs a boy directly left or right, placed only in empty seats. Return the minimum boys or -1 if it's impossible. It looks like a DP problem, but it isn't. If you're taking this OA soon, learn the left-to-right greedy and the one edge case that returns -1. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this pattern is short enough to own tonight.
The problem
You are given a row of seats represented by a string s. 'G' means the seat is already occupied by a girl. '-' means the seat is empty, and you may place a boy there. Place the minimum number of boys so that every girl has at least one adjacent boy immediately to her left or right. Boys can only be placed in empty seats. Return the minimum number of boys required. If it is impossible to satisfy every girl, return -1. Function minBoysNextToGirls(s: String) → int Examples Example 1 s = "-G-GG--" return = 2 One optimal placement is -GBGGB- with boys at indices 2 and 5. Example 2 s = "G-G" return = 1 Placing one boy in the middle seat covers both girls. Example 3 s = "G" return = -1 Constraints 1 <= s.length <= 2 * 10^5 s[i] is either 'G' or '-'. Adjacency only means immediate left or immediate right.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a left-to-right greedy. Scan the string. When you hit an uncovered girl, place a boy on her right if that seat is empty, because it might also cover the next girl. If the right seat is a girl or off the board, place on her left if it's empty. If neither works, return -1. That happens for a lone G, or a run of girls with no empty seat beside a girl, like "GG". The pitfall is placing left first, which wastes coverage and overcounts. Example 1 shows it: -GBGGB- works with two boys because the boy at index 2 covers the girls at indices 1 and 3. Track a covered flag per girl, or mark placed boys in an array. It's O(n) time, which matters with n up to 2 * 10^5. If you freeze on the live OA, StealthCoder can hand you this greedy while you verify the edge cases.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Minimum Boys Next to Girls cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderYou've seen the question.
Make sure you actually pass Guidewire's OA.
Guidewire reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Boys Next to Girls FAQ
What's the trick for Minimum Boys Next to Girls?+
Greedy from left to right. For each uncovered girl, prefer placing a boy on her right empty seat, since it can also cover the next girl. Only fall back to the left seat if the right is blocked. If neither side is empty, return -1.
How hard is this problem really?+
Easy to medium. There's no heavy data structure, just a linear scan and careful edge handling. Most people lose points on the impossible case or on choosing left over right. If you trace the three examples by hand, you'll catch both mistakes.
When does the answer return -1?+
When some girl has no empty seat adjacent to her. A single 'G' is the clear case, and a string like "GG" also fails because neither girl has an empty neighbor. Check both neighbors for every uncovered girl during the scan.
Why not use dynamic programming here?+
You can, with states for whether the previous seat holds a boy and whether the last girl is covered. But the greedy is simpler and just as fast. With n up to 2 * 10^5, either works in O(n), so pick the one you can code without bugs.
How do I prep for this in 48 hours?+
Code the greedy once from scratch and run the three given examples. Then write your own tests like "G-G-G", "GG-", and "-G-". Spend the rest of the time on similar interval-style greedy problems. Guidewire reported this in May 2026, so it's recent.