Amazon Interview Question for SDE1s


Country: United States




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

C++ Solution.
Assumption: int m (level) is also the limit length for the key. If this assumption is not agreeable, it's easy to increase the level and filter strings that have more than m of the same character....

#include <iostream>
#include <string>
#include <set>

using namespace std;

// Recursive Function to generate IDs for one level.
set<string> makeIdsForLevel(set<string> dict, int level){
    if(level == 1){
        return dict;
    }

    set<string> mergedSet;
    for(auto its1 : dict)
        for(auto its2 : makeIdsForLevel(dict,level-1))
            mergedSet.insert(its1+its2);

    return mergedSet;
}

//Wrapper function.
set<string> makeUniqueIDs(set<char> charDict, int maxRep){

    set<string> strDict;
    for(auto c : charDict)
        strDict.insert(string(1,c));

    set<string> ids;
    for(int i=1; i<=maxRep; ++i){
        auto idLevel = makeIdsForLevel(strDict,i);
        ids.insert(idLevel.begin(), idLevel.end());
    }
    return ids;
}

//Comparator to order set by length of ID
struct CompareLenght
{
    bool operator()(const string& s1, const string& s2) {
        return (s1.length() <= s2.length());
    }
};

// Wrapper Class around ID generator.
// The set is sorter in increasing length order.
// next() always returns the next available ID then deletes it.
class IdGenerator{
    set<string,CompareLenght> ids;
public:
    IdGenerator(set<char> charDict, int level){
        auto idsUnordered = makeUniqueIDs(charDict,level);
        for(auto id : idsUnordered)
            ids.insert(id);

        cout << "Constructed Ids: [" << endl;
        for(auto id : ids)
            cout << id << " ";
        cout << "]" << endl;
    }
    string next(){
        if(ids.begin()==ids.end()) return "";

        string first = *ids.begin();
        ids.erase(ids.begin());
        return first;
    }
};

int main()
{
    set<char> charDict = {'1','2', '3'};

    IdGenerator idGen(charDict,3);

    string id = idGen.next();
    while(!id.empty()){
        cout << "next id: " << id << endl;
        id = idGen.next();
    }

    cout << "All IDs have been used.";

    return 0;
}

OUTPUT:

Constructed Ids: [
3 2 1 33 32 31 23 22 21 13 12 11 333 332 331 323 322 321 313 312 311 233 232 231 223 222 221 213 212 211 133 132 131 123 122 121 113 112 111 ]
next id: 3
next id: 2
next id: 1
next id: 33
next id: 32
next id: 31
next id: 23
next id: 22
next id: 21
next id: 13
next id: 12
next id: 11
next id: 333
next id: 332
next id: 331
next id: 323
next id: 322
next id: 321
next id: 313
next id: 312
next id: 311
next id: 233
next id: 232
next id: 231
next id: 223
next id: 222
next id: 221
next id: 213
next id: 212
next id: 211
next id: 133
next id: 132
next id: 131
next id: 123
next id: 122
next id: 121
next id: 113
next id: 112
next id: 111
All IDs have been used.
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.

- Gabriel Feyer May 23, 2018 | 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