Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

//java 8
public class MatchPatternOnDictionary {

    public static void main(String[] args) {
        MatchPatternOnDictionary w= new MatchPatternOnDictionary ();
        List<String> dict= Arrays.asList( new String [] {"cat", "rat", "mat", "apple", "boy", "bat"});
        final String pat =".at";
        Predicate<String> f = w.predicateFilter(pat);
        List <String> result = dict.stream().filter(f).collect(Collectors.toList());
        System.out.println(result);
        System.out.println("total matches "+ result.size());


    }

    Predicate<String> predicateFilter (String pat) {
        return  (x)-> Pattern.compile(pat).matcher (x).matches ();
    }
}

- Makarand August 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I assume '?' means "any one character".

#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

class TrieNode {
	public:
		TrieNode()
		{
			terminal_ = false;
		}
		~TrieNode()
		{
			for (auto &child : children_) {
				delete child.second;
			}
		}
		TrieNode *AddChild(char ch)
		{
			TrieNode *child = children_[ch];
			if (!child) {
				child = new TrieNode();
				children_[ch] = child;
			}
			return child;
		}
		TrieNode const *Child(char ch) const
		{
			auto it = children_.find(ch);
			return it != children_.end() ? it->second : NULL;
		}
		unordered_map<char, TrieNode *> const &Children() const
		{
			return children_;
		}
		void Terminal(bool terminal)
		{
			terminal_ = terminal;
		}
		bool Terminal() const
		{
			return terminal_;
		}

	private:
		unordered_map<char, TrieNode *> children_;
		bool terminal_;
};

class Trie {
	public:
		void Add(string const &s)
		{
			TrieNode *n = &root_;
			for (auto const &c : s) {
				n = n->AddChild(c);
			}
			n->Terminal(true);
		}
		TrieNode *Root()
		{
			return &root_;
		}

	private:
		TrieNode root_;
};

void Search(TrieNode const *n, string const &pattern, vector<string> &out, string word = "", int idx = 0)
{
	if (idx < 0 ||
		idx > pattern.size() ||
		!n)
	{
		return;
	}
	if (idx == pattern.size()) {
		if (n->Terminal()) {
			out.push_back(word);
		}
		return;
	}
	if (pattern[idx] == '?') {
		for (auto &el : n->Children()) {
			char ch = el.first;
			TrieNode const *child_node = el.second;
			Search(child_node, pattern, out, word + ch, idx + 1);
		}
	} else {
		Search(n->Child(pattern[idx]), pattern, out, word + pattern[idx], idx + 1);
	}
}

int main(int argc, char const **argv)
{
	vector<string> const dict = {"cat", "rat", "mat", "apple", "boy", "bat"};

	Trie trie;
	for (string const &word : dict) {
		trie.Add(word);
	}

	vector<string> out;
	Search(trie.Root(), "?at", out);
	for (auto &s : out) {
		cout << s << "\n";
	}

	return 0;
}

- Alex November 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It's a clear application of TRIE and DFS

- koustav.adorable August 31, 2017 | 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