John has a string, consisting of n English alphabetic letters. He needs to compact the string such that any adjacent pairs of letters will be represented as a single character.
Input:aabbccabddf, Output: abcabdf
Input:aabbaa, Output: aba
First step should be to demonstrate like you understood the task. You can write your own example and ask questions - did you understand ?
You should start to ask right question:
- is the string case sensitive?
- should we validate the string?
- what we should return after validation? IllegalArgumentException?
Next step is explain your approach. If you will use pseudo code or will explain the technology you will be use - it would be perfect start.
What is the goal of this interview? to show you are familiar with the best coding practice :
- coding style,
- one method - one responsibility,
- security understanding (private/public),
- knowledge of the most efficient way to resolve / about libraries,
- thinking about testing and validation,
- hands-on coding development.
If you could you features of java 8+, junit library to test - it would be good sign for you.
In the example we provided you could show your knowledge of regular expression.
private static String compactString(String s){ while(true){ int length = s.length(); s=s.replaceAll("(.)(\\1+)","$1"); if (s.length()==length) { break; } return s; } }