Uber Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
#include <iostream>
#include <set>
using namespace std;
class Id {
private:
int id;
std::set<int> unack_ids;
public:
Id () : id (0), unack_ids() { }
int getNextId () {
++id;
unack_ids.insert(id);
return id;
}
void acknowledge ( int i) {
cout << "acknowledge " << i << endl;
unack_ids.erase(i);
}
int getIdLevel() {
if ( unack_ids.empty() ) {
return -1;
}
return *unack_ids.begin();
}
};
int main() {
Id id;
for ( int i = 0; i < 10; ++ i ) {
cout << id.getNextId() << std::endl;
}
id.acknowledge(5);
cout << id.getIdLevel() << endl;
id.acknowledge(6);
cout << id.getIdLevel() << endl;
id.acknowledge(8);
cout << id.getIdLevel() << endl;
id.acknowledge(0);
cout << id.getIdLevel() << endl;
id.acknowledge(1);
cout << id.getIdLevel() << endl;
id.acknowledge(4);
cout << id.getIdLevel() << endl;
id.acknowledge(2);
cout << id.getIdLevel() << endl;
id.acknowledge(3);
cout << id.getIdLevel() << endl;
id.acknowledge(7);
cout << id.getIdLevel() << endl;
id.acknowledge(9);
cout << id.getIdLevel() << endl;
id.acknowledge(10);
cout << id.getIdLevel() << endl;
return 0;
}
#include <iostream>
#include <set>
using namespace std;
class Id {
private:
int id;
std::set<int> unack_ids;
public:
Id () : id (0), unack_ids() { }
int getNextId () {
++id;
unack_ids.insert(id);
return id;
}
void acknowledge ( int i) {
cout << "acknowledge " << i << endl;
unack_ids.erase(i);
}
int getIdLevel() {
if ( unack_ids.empty() ) {
return -1;
}
return *unack_ids.begin();
}
};
int main() {
Id id;
for ( int i = 0; i < 10; ++ i ) {
cout << id.getNextId() << std::endl;
}
id.acknowledge(5);
cout << id.getIdLevel() << endl;
id.acknowledge(6);
cout << id.getIdLevel() << endl;
id.acknowledge(8);
cout << id.getIdLevel() << endl;
id.acknowledge(0);
cout << id.getIdLevel() << endl;
id.acknowledge(1);
cout << id.getIdLevel() << endl;
id.acknowledge(4);
cout << id.getIdLevel() << endl;
id.acknowledge(2);
cout << id.getIdLevel() << endl;
id.acknowledge(3);
cout << id.getIdLevel() << endl;
id.acknowledge(7);
cout << id.getIdLevel() << endl;
id.acknowledge(9);
cout << id.getIdLevel() << endl;
id.acknowledge(10);
cout << id.getIdLevel() << endl;
return 0;
}
- rajivgadda October 22, 2017