Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

I guess you are given N=string length, where n1+n2+n3 = N/2.

Solution is to backtrack-> modify a recent problem involving only n3 (yesterday).

- S O U N D W A V E October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

amazing

- Anonymous October 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I know that this solution is inefficient. But I think that it works, anyway. Execute it with the argument "3", e.g. ./a.out 3

#include <iostream>
#include <vector>
#include <tr1/unordered_set>
#include <string>
#include <cstdlib>

using namespace std;
using namespace std::tr1;

static unordered_set<string> parencomb(vector<string>& paren, int n) {

    unordered_set<string> current, prev, result;

    string null;
    current.insert(null);

    int level = 0;
    while (level < n) {
        for (unordered_set<string>::iterator i = current.begin();
             i != current.end();
             i++) {

            for (vector<string>::iterator j = paren.begin();
                 j != paren.end();
                 j++) {

                if (0 == i->size() || i->find((*j)[0]) == string::npos) {
                    prev.insert(*j + *i);
                    prev.insert(*i + *j);
                    prev.insert((*j)[0] + *i + (*j)[1]);
                    
                    int size = i->size();
                    if (size != 0 && size % 2 == 0) {
                        int middle = size/2;
                        prev.insert(i->substr(0, middle) + *j + i->substr(middle, size-middle));
                    }
                }
            }
        }
        result.insert(prev.begin(), prev.end());
        current = prev;
        level++;
    }

    return result;
}

int main(int argc, char* argv[]) {

    if (argc != 2) {
        cout << argv[0] << " <level>" << endl;
        exit(0);
    }

    int n = atoi(argv[1]);
    
    vector<string> paren;
    paren.push_back("{}");
    paren.push_back("[]");
    paren.push_back("()");

    unordered_set<string> result = parencomb(paren, n);
    for (unordered_set<string>::iterator i = result.begin();
         i != result.end();
         i++) {
        cout << *i << endl;
    }
}

- Chiharu October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

static void doBracketPairs(StringBuilder sb, int chaves, int colchetes, int parentesis)
        {
            int i = 0;
            do
            {
                if (parentesis != 0)
                {
                    doBracketPairs(sb.Insert(i, "()"), chaves, colchetes, parentesis - 1);
                    sb.Remove(i, 2);
                }
                if (colchetes != 0)
                {
                    doBracketPairs(sb.Insert(i, "[]"), chaves, colchetes - 1, parentesis);
                    sb.Remove(i, 2);
                }
                if (chaves != 0)
                {
                    doBracketPairs(sb.Insert(i, "{}"), chaves - 1, colchetes, parentesis);
                    sb.Remove(i, 2);
                }
            } while (i++ < sb.Length);

            if ((parentesis == 0) && (colchetes == 0) && (chaves == 0))
            {
                Console.WriteLine(sb);
            }
        }

        static void BracketPairs(int chaves, int colchetes, int parentesis)
        {
            StringBuilder sb = new StringBuilder();
            doBracketPairs(sb, chaves, colchetes, parentesis);
        }

- it prints duplicates... we can usa a set to avoit that... but its late... October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.


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