Full Stack Developer (Java) Question: Download Full Stack Developer (Java) PDF

Can you write a function to detect if two strings are anagrams (for example, SAVE and VASE)?

Tweet Share WhatsApp

Answer:

This is my go-to first interview question. It helps me gauge a candidate’s ability to understand a problem and write an algorithm to solve it.

If someone has not solved the problem before, I expect to see some code with loops and if/then’s. Maybe some HashMaps. The things I look for are ability to break down the problem to see what you need to check, what the edge cases are, and whether the code meets those criteria.

The naive solution is often to loop through the letters of the first string and see if they’re all in the second string. The next thing to look for is, the candidate should also do that in reverse too (check string 1 for string 2’s letters)? The next thing to look for is, what about strings with duplicate letters, like VASES?

Download Full Stack Developer (Java) PDF Read All 43 Full Stack Developer (Java) Questions
Previous QuestionNext Question
Tell me how do you run a Java application on the command line and set the classpath with multiple jars?Tell me what's your favorite language, and why?