So I had a job interview two days ago and they used coderPad.io for it, which is pretty common for job interviews. As a matter of fact, I have another job interview coming up that uses coderPad as well, so I really need to ask this question.
Essentially what happened was that my algorithm was written correctly. My interviewer told me so. However, the hash map was not working and we started debugging until the interviewer got tired and ended the interview right there. I then received a rejection email a day later. The interviewer did however narrow it down to the insert function on the hash map. We tried different ways of inserting and it still did now work.
I had to write an algorithm that needed for me to find the frequency for every integer element in a vector. However, when I had print the contents of the hash map, the frequency is always 1 for each element when it is not supposed to be 1 for each element. This had cost me the interview process to continue. I have recreated the algorithm on coderPad just now and the same issue is occurring. Here is the code:
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
// To execute C , please define "int main()"
class hashMapTester {
public:
hashMapTester() {
}
unordered_map<int, int> collectMap(vector<int>& arr) {
unordered_map<int, int> map;
for (long unsigned int i = 0; i < arr.size(); i ) {
if (map.find(arr[i]) != map.end()) {
auto freq = map.find(arr[i])->second;
freq ;
map.insert(pair<int, int> (arr[i], freq));
} else {
map.insert(pair<int, int>(arr[i], 1));
}
}
return map;
}
void printMap(unordered_map<int, int> map, vector<int>& arr) {
for (const auto& iter : map) {
cout << iter.second << endl;
}
}
};
int main() {
vector<int> arr = {1, 2, 2, 3 , 4 , 4, 4};
hashMapTester hM;
unordered_map<int, int> map = hM.collectMap(arr);
hM.printMap(map, arr);
return 0;
}
Why is the frequency portion of the map always outputting 1 when it is not supposed to ? I am stuck on this and I really need to understand why. When I use this algorithm on LeetCode or on another compiler, it works, but not on CoderPad. Can anyone please help me out ? What do I need to do to make it work on CoderPad ?
CodePudding user response:
Per https://en.cppreference.com/w/cpp/container/unordered_map/insert, the insert
method "inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key."
The call to insert
in the following section won't actually change the contents of the unordered_map
.
if (map.find(arr[i]) != map.end()) {
auto freq = map.find(arr[i])->second;
freq ;
map.insert(pair<int, int> (arr[i], freq)); // <<-- here
}
Option 1: Make freq
a reference
if (map.find(arr[i]) != map.end()) {
auto& freq = map.find(arr[i])->second;
freq ;
}
Option 2: Simplify the algorithm,
unordered_map<int, int> collectMap(const vector<int>& arr) {
unordered_map<int, int> map;
for (int val : arr) {
map[val];
}
return map;
}
CodePudding user response:
To quote cppreference:
Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.
You should probably use operator[]
instead.