CoderPad C++ Hashmap did not work during job interview. Can you explain to me why?
NickName:Saturnsbelt Ask DateTime:2022-11-04T22:08:18

CoderPad C++ Hashmap did not work during job interview. Can you explain to me why?

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 ?

Copyright Notice:Content Author:「Saturnsbelt」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74318341/coderpad-c-hashmap-did-not-work-during-job-interview-can-you-explain-to-me-wh

Answers
MarkB 2022-11-04T14:20:53

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."\nThe call to insert in the following section won't actually change the contents of the unordered_map.\nif (map.find(arr[i]) != map.end()) {\n auto freq = map.find(arr[i])->second;\n freq++;\n map.insert(pair<int, int> (arr[i], freq)); // <<-- here\n}\n\nOption 1: Make freq a reference\nif (map.find(arr[i]) != map.end()) {\n auto& freq = map.find(arr[i])->second;\n freq++;\n}\n\nOption 2: Simplify the algorithm,\nunordered_map<int, int> collectMap(const vector<int>& arr) {\n unordered_map<int, int> map;\n for (int val : arr) {\n ++map[val];\n }\n return map;\n}\n",


More about “CoderPad C++ Hashmap did not work during job interview. Can you explain to me why?” related questions

Writing SQL in Coderpad

I have an interview coming up in which I will write code in Coderpad. What is the syntax to write SQL in Coderpad? (note Coderpad with an "r", not Codepad). First, you select a language for Coder...

Show Detail

Is it possible to set breakpoints in CoderPad for C#?

Taking an interview within CoderPad for c# and wondering if we can set breakpoints for easy debugging. I looked at the Sandbox they provide and I don't see any obvious way to do it. Are we only lef...

Show Detail

What is the name of the theme used by coderpad

I can't seem to find a defined theme used by Coderpad by default see here https://coderpad.io/sandbox The closest thing I found was https://vscodethemes.com/e/SarahRidge.vscode-monokai-minimal how...

Show Detail

How to use coderpad and swift?

I am preparing for a technical interview coming up soon and the online IDE is coderpad. I have been trying to run my code for sometime now and it is simply not printing anything. I know the solutio...

Show Detail

Is it possible to use the ES6 keyword `import` in CoderPad?

Background I'm trying to import the underscore library within coderpad.io (aka CoderPad) This ES5 syntax works: const _ = require('underscore') This ES6 syntax doesn't: import * as _ from &#

Show Detail

What happens when Coderpad creates an interview session?

I am trying to understand on a high level how a system like coderpad works. Everytime I use Coderpad to practice interviews with friends, it creates a session with a temporary link that both users ...

Show Detail

coderpad.io disable blinking cursor

I am looking to disable the blinking cursor in coderpad.io I think its a codeMirror editor. I am using CSS .CodeMirror-cursor, div.CodeMirror-cursor { opacity: 1; display: none; } But t...

Show Detail

How to import npm javascript Priority Queue library when interviewing with coderpad

so Leetcode already supports @datastructures-js/priority-queue which is why I could just use let heap = new MinPriorityQueue() out of the box. But then I realized when I interview with coderpad or

Show Detail

CoderPad C++ Hashmap did not work during job interview. Can you explain to me why?

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 ...

Show Detail

Using CoderPad, why do I get a NoSuchElementException using a Scanner on System.in?

I am preparing for an interview I have in a few days. I was told that I should familiarize myself with CoderPad since that is the collaborator I will be interviewed with. However, I was in the midd...

Show Detail