What to do when you cannot think of the best approach in DSA problems
Hey folks š
Iām a Java backend developer with ~3.5 YOE, currently prepping seriously for coding interviews (DSA + system design). I recently solved a LeetCode problem and wanted to get some perspective from more experienced folks here.
problem: 1189. Maximum Number of Balloons
š§© Problem Context
Itās the classic āmaximum number of āballoonā instances from a stringā type problem.
š” My Approach (Accepted but not optimal)
I built a frequency map and then kept subtracting characters for "balloon" in a loop until I couldnāt anymore.
public int maxNumberOfBalloons(String text) {
Map<Character,Integer> have= new HashMap<>();
for(int i=0;i<text.length();i++){
have.put(text.charAt(i),have.getOrDefault(text.charAt(i),0)+1);
}
String need="balloon";
int res=0;
while(true){
for(int i=0;i<need.length();i++){
char c = need.charAt(i);
if (!have.containsKey(c) || have.get(c) == 0) {
return res;
}
have.put(c, have.get(c) - 1);
if (have.get(c) == 0) {
have.remove(c);
}
}
res++;
}
}
š¤ My Question
I know this works, but itās clearly not the most optimal / elegant solution.
š In interviews or practice:
- What do you do when you canāt immediately think of the optimal solution?
- Is it okay to start with a brute-force or suboptimal approach like this and then improve?
- How do you train yourself to recognize patterns faster (like frequency counting optimizations here)?
š What Iām Trying to Improve
- Moving from āworking solutionā ā āoptimal solutionā faster
- Recognizing patterns under time pressure
- Communicating thought process clearly
Would really appreciate how you all approach this in real interviews š.
P.S. I used ChatGPT to help draft this post. Donāt worry I am still human just getting some backup from my AI friend while preparing.
Posted Apr 2, 2026, 11:21 AM UTC