Amazon Interview Question for SDE-2s


Country: United States




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

We can create a Ternary search tree Or a Trie structure for dictionary word.
And can find out the words starting with part before *.
Later filter these words with the end part of the pattern.

- rvedpathak4u July 31, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use dictionaries for this..where key is D*sk and we can get the value for * dynamically. Then match for the word in dictionary.

- manasa August 09, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
use a Trie structure where each node has a character and has a list of nodes.
*/

// iterative
class WordDictionary {
    class Node {
        Node[] letters;
        boolean endOfWord;
        
        public Node() {
            letters = new Node[26];
            endOfWord = false;
        }
    }

    Node head;

    /** Initialize your data structure here. */
    public WordDictionary() {
        head = new Node();
    }

    /** Adds a word into the data structure. */
    public void addWord(String word) {
        if (word == null) {
            return;
        }
        
        Node temp = head;
        
        for (int i = 0; i < word.length(); ++i) {
            if (temp.letters[word.charAt(i) - 'a'] == null) {
                temp.letters[word.charAt(i) - 'a'] = new Node();
            }
            
            temp = temp.letters[word.charAt(i) - 'a'];
            
            if (i == word.length() - 1) {
                temp.endOfWord = true;
            }
        }
        
        return;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        if (word == null) {
            return false;
        }
        
        // bfs - iterative
        Queue<Node> q = new LinkedList<>();
        q.add(head);
        
        for (int i = 0; i < word.length(); ++i) {
            int size = q.size();
            if (size == 0) {
                return false;
            }
            
            if (word.charAt(i) == '.') {
                while (size > 0) {
                    Node temp = q.remove();
                    Node[] tempLetters = temp.letters;
                    
                    for (Node letter : tempLetters) {
                        if (letter != null) {
                            q.add(letter);
                        }
                    }
                    
                    --size;
                }
            }
            else {
                while (size > 0) {
                    Node temp = q.remove();
                    Node[] tempLetters = temp.letters;
                    
                    if (tempLetters[word.charAt(i) - 'a'] != null) {
                        q.add(tempLetters[word.charAt(i) - 'a']);
                    }
                    
                    --size;
                }
            }
        }
        
        while (!q.isEmpty()) {
            Node temp = q.remove();
            
            if (temp.endOfWord) {
                return true;
            }
        }
        
        return false;
    }
}

- Euihoon Seol October 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// recursive
class WordDictionary {
    class Node {
        Node[] letters;
        boolean endOfWord;
        
        public Node() {
            letters = new Node[26];
            endOfWord = false;
        }
    }

    Node head;

    /** Initialize your data structure here. */
    public WordDictionary() {
        head = new Node();
    }

    /** Adds a word into the data structure. */
    public void addWord(String word) {
        if (word == null) {
            return;
        }
        
        addWordUtil(word, 0, word.length() - 1, head);
        
        return;
    }

    public void addWordUtil(String word, int currIdx, int endIdx, Node n) {
        if (currIdx < endIdx) {
            if (n.letters[word.charAt(currIdx) - 'a'] == null) {
                n.letters[word.charAt(currIdx) - 'a'] = new Node();
            }
            addWordUtil(word, currIdx + 1, endIdx, n.letters[word.charAt(currIdx) - 'a']);
        }
        else if (currIdx == endIdx) {
            if (n.letters[word.charAt(currIdx) - 'a'] == null) {
                n.letters[word.charAt(currIdx) - 'a'] = new Node();
            }
            n.letters[word.charAt(currIdx) - 'a'].endOfWord = true;
        }
        
        return;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        if (word == null) {
            return false;
        }
        
        // dfs - recursive
        return searchUtil(word, 0, word.length() - 1, head);
    }
    
    public boolean searchUtil(String word, int currIdx, int endIdx, Node currNode) {
        if (currIdx < endIdx) {
            if (word.charAt(currIdx) == '.') {
                for (Node letter : currNode.letters) {
                    if (letter != null && searchUtil(word, currIdx + 1, endIdx, letter)) {
                        return true;
                    }
                }
            }
            else {
                if (currNode.letters[word.charAt(currIdx) - 'a'] == null) {
                    return false;
                }
                else {
                    return searchUtil(word, currIdx + 1, endIdx, currNode.letters[word.charAt(currIdx) - 'a']);
                }
            }
        }
        else if (currIdx == endIdx) {
            if (word.charAt(currIdx) == '.') {
                for (Node letter : currNode.letters) {
                    if (letter != null && letter.endOfWord) {
                        return true;
                    }
                }
            }
            else {
                if (currNode.letters[word.charAt(currIdx) - 'a'] != null && currNode.letters[word.charAt(currIdx) - 'a'].endOfWord) {
                    return true;
                }
            }
        }
        
        return false;
    }
}

- Euihoon Seol October 18, 2020 | Flag Reply


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