Amazon Interview Question for SDE1s


Country: United States




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

public static String removeRedundantChars(char[] input){
        //assume ASCII characters only 
        boolean[] charTable = new boolean[128];
        int i = 0;
        for(int j = 0; j < input.length; j++){
            if (!charTable[input[j]])
            {
                charTable[input[j]] = true;
                input[i] = input[j];
                i++;
            }
        }
        return new String(input, 0, i);
    }

- Anonymous January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

This solution takes O(1) space - BitSet and result StringBuilder:

public class RedundantCharacters {

    public static void main(String[] args) {
        new RedundantCharacters().solve();
    }

    public void solve() {
        String s = "qwertyasdfghytrewq";

        System.out.print(removeRedundant(s));
    }

    public String removeRedundant(String s) {
        StringBuilder sb = new StringBuilder();

        BitSet bitSet = new BitSet();

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!bitSet.get(ch)) {
                sb.append(ch);
            }

            bitSet.set(ch);
        }

        return sb.toString();
    }
}

- krylloff January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Using bit set is O(n) and not O(1) space complexity. Bitset is nothing but array of bits internally.

As far as i see, there needs to be a tradeoff between space and time complexity. Best is [Time complexity vs Space complexity] = [O(n) vs O(n)] or [O(nlogn) vs O(1)]

- Anonymous January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The space compexity of this approach is O(A), Where A is capacity of alphabet for given string.

- krylloff January 14, 2014 | Flag


Add a Comment
Name:

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

Books

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

Videos

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