Showing posts with label interview. Show all posts
Showing posts with label interview. Show all posts

Friday, March 7, 2014

Detecting if a string has unique characters: comparing my solution to “Cracking the Coding Interview?”

There are two separate questions here: what's the efficiency of your solution, and what is the reference solution doing? Let's treat each independently.
First, your solution:
public static boolean checkForUnique(String str){
    boolean containsUnique = false;

    for(char c : str.toCharArray()){
        if(str.indexOf(c) == str.lastIndexOf(c)){
            containsUnique = true;
        } else {
            containsUnique = false;
        }
    }

    return containsUnique;
}
Your solution essentially consists of a loop over all characters in the string (let's say there are n of them), checking on each iteration whether the first and last index of the characters are the same. The indexOf and lastIndexOf methods each take time O(n), because they have to scan across all the characters of the string to determine if any of them match the one you're looking for. Therefore, since your loop runs O(n) times and does O(n) work per iteration, its runtime is O(n2).
However, there's something iffy about your code. Try running it on the string aab. Does it work correctly on this input? As a hint, as soon as you determine that there are two or more duplicated characters, you're guaranteed that there are duplicates and you can return that not all characters are unique.
Now, let's look at the reference:
public static boolean isUniqueChars(String str) {
    if (str.length() > 256) { // NOTE: Are you sure this isn't 26?
        return false;
    }
    int checker = 0;
    for (int i = 0; i < str.length(); i++) {
        int val = str.charAt(i) - 'a';
        if ((checker & (1 << val)) > 0) return false;
        checker |= (1 << val);
    }
    return true;
}
This solution is cute. The basic idea is the following: imagine that you have an array of 26 booleans, each one tracking whether a particular character has appeared in the string already. You start with all of them false. You then iterate across the characters of the string, and each time you see a character you look into the array slot for that character. If it's false, this is the first time you've seen the character and you can set the slot to true. If it's true, you've already seen this character and you can immediately report that there's a duplicate.
Notice that this method doesn't allocate an array of booleans. Instead, it opts for a clever trick. Since there are only 26 different characters possible and there are 32 bits in an int, the solution creates an int variable where each bit of the variable corresponds to one of the characters in the string. Instead of reading and writing an array, the solution reads and writes the bits of the number.
For example, look at this line:
if ((checker & (1 << val)) > 0) return false;
What does checker & (1 << val) do? Well, 1 << val creates an int value that has all bits zero except for the valth bit. It then uses bitwise AND to AND this value with checker. If the bit at position val in checker is already set, then this evaluates to a nonzero value (meaning we've already seen the number) and we can return false. Otherwise, it evaluates to 0, and we haven't seen the number.
The next line is this:
checker |= (1 << val);
This uses the "bitwise OR with assignment" operator, which is equivalent to
checker = checker | (1 << val);
This ORs checker with a value that has a 1 bit set only at position val, which turns the bit on. It's equivalent to setting the valth bit of the number to 1.
This approach is much faster than yours. First, since the function starts off by checking if the string has length greater than 26 (I'm assuming the 256 is a typo), the function never has to test any string of length 27 or greater. Therefore, the inner loop runs at most 26 times. Each iteration does O(1) work in bitwise operations, so the overall work done is O(1) (O(1) iterations times O(1) work per iteration), which is significantly faster than your implementation.
If you haven't seen bitwise operations used this way, I'd recommend searching for "bitwise operators" on Google to learn more.
Hope this helps!

Sunday, April 12, 2009

28 Questions You Wish You Asked the Manager During the Job Interview

About the Team
1. Who is my line manager or the project leader to whom I will report? Can I meet him? (Some companies only let candidates meet the managers. Go figure.)
2. Before I accept the job, can we do a lunch with the whole team to get to know each other?
3. What is the demographic of the team? or Why are there so few women in senior positions here? (Diversity matters.)
4. What sort of team environment do you promote? How do teams interact with one another?

About the manager
5. How much time do you spend in one-on-one meetings versus the time in team meetings?
6. Give an example of something unexpected and how it was handled.
7. How do you measure success? Where does that measurement stand now? What actions are you taking to change the measurement(s) (in the right direction)? What have those actions done to the measurement(s)?
8. How does your manager measure your success?
9. What is your preferred method of communication? Phone calls, e-mail, informally, in meetings, only when necessary? How much contact will I have with you?
10. Why do people typically leave your team? (The best answer probably is that they were promoted, but if they're leaving the company or seeking internal transfers you may have an opportunity to drill down and find out why. This is also a subtle way to ask how long the manager has been managing people.)

About the work environment
11. Can you show me where I'll be working? Can we walk around the office? (As you walk around, listen.)
12. Are the tools I'll be using cutting edge or totally ancient? What is the specification of the developer machines that you provide?
13. What sort of training and development mechanisms are there for professional development? How about conference attendance? (Factor in the answer when you begin negotiating. Plus you want to know if the company is interested in investing in people.)
14. Are there opportunities to explore my skills at different business areas through the course of my tenure? (Can you grow in different areas, and move into a new one? For examples, can testers with a bend towards coding become developers?)
15. I'm not a morning person. Will I have flexibility to work when I am most productive, barring things like mandatory meetings?
16. What is our mission? What will be my role in the mission?
17. How many positions am I actually covering?
18. What will I be expected to accomplish the first three months?
19. What projects will I work on? How will I be transitioned to new projects after existing projects are over?
20. How much overtime has this team been doing in the last three months? What's typical? What's acceptable? How does the company respond after a time-crunch is over? (If you ask directly, "Do you encourage work/life balance?" naturally they'll respond "Sure!" Instead, ask a specific question to find out if that "Sure!" matches reality.)

The company
21. What is your company vision? How do you reflect it in your daily work? (Most start-ups have big ideas but few conduct themselves in a way that will help them reach their goals.)
22. How do benefits compare to sector averages?
23. Could you explain the pay review system? Also, the performance review system?
24. How comfortable are you with your company's financials? How has the current economic climate impacted business?
25. When did the company have its last layoff? (What you really want to know, of course, is "How long until the most recently hired get laid off?")

The open-ended questions
26. What one thing would you change about working here? You can ask this of anyone on the team. The answer tells you a lot about the workplace, as well as the values of the person you're talking to.
27. What's the best thing about working here? What's the worst thing about working here? Expect that the "best" things will be unremarkable, however nice to hear (such as "so many smart people!"). Listen carefully to the undertone in the worst-things.
28. In six months, what will I love about working for you? What will I hate? (I learned this question during researching the aforementioned DevSource article, and it's been amazingly useful. Warning: if the manager says, "You won't hate anything! I'm a nice guy!" run away. Either he has poor self-assessment skills, or he has demonstrated that he'll never give you a straight and honest answer.)

http://www.javaworld.com/community/node/2745