Interview Question

Country: India

Comment hidden because of low score. Click to expand.
of 0 vote

Take a look at this problem this way:

For instance take a look at 3rd case: ababab [4, 2]
The possible outcomes are 111100, 111001.......(just replace 1 at ith position with char[i]), we get abab, abaa.......

If you see all we need to do is find all the anagrams of 111100 (and the word here has repeated characters).

An example solving of such anagram can be [consider aabc] and we already have anagrams of abc (i.e. abc, acb, bac, cab, bca, cba) and we need to now merge a into these 6 guys (to get anagrams of aabc)

This can be done as: _a_bc, _a_cb, _b_ac, _ba_c...........
When doing this, if we check if last character of 1st substring or first character of last substring is same as character to be merged then we get a duplicate case. For all other cases, merge the character as normal.

The same principle can be applied to the problem above

- ashu February 19, 2012 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More