What is the best C++ data structure that could be used for storing and managing a collection of integers?

Muhamad Gafar

this is my first StackOverflow question so please let me know if I didn't follow community guidelines with this question and if I should delete it.

I got my first ever interview question and I got rejected because of my implementation.

The question is:

Design and implement a C++ class that stores a collection of integers. On construction, the collection should be empty. The same number may be stored more than once.

Implement the following methods:

  1. Insert(int x). Insert an entry for the value “x”.

  2. Erase(int x). Remove one entry with the value “x” (if one exists) from the collection.

  3. Erase(int from, int to). Remove all the entries with a value in the range [from, to).

  4. Count(int from, int to). Count how many entries have a value in the range [from, to).

I thought a good implementation would be to use linked lists since it uses non-contiguous memory and removing entries would not require shuffling a lot of data (like in vectors or arrays). However, I got feedback from the company saying my implementation was O(n^2) time complexity and was very inefficient so I was rejected. I don't want to repeat the same mistake if a similar question pops up in another interview so I'd like to know what is the best way to approach this question (a friend suggested using maps but he is also unsure).

My code is:

void IntegerCollector::insert(int x)
{
    entries.push_back(x);
}

void IntegerCollector::erase(int x)
{
    list<int>::iterator position = find(entries.begin(), entries.end(), x);
    if (position != entries.end())
        entries.erase(position);
}

void IntegerCollector::erase(int from, int to)
{
    list<int>::iterator position = entries.begin();

    while (position != entries.end())
    {
        if (*position >= from && *position <= to)
            position = entries.erase(position);
        else
            position++;
    }
}

int IntegerCollector::count(int from, int to)
{
    list<int>::iterator position = entries.begin();
    int count = 0;

    while (position != entries.end())
    {
        if (*position >= from && *position <= to)
            count++;

        position++;
    }

    return count;
}

The feedback mentioned that they would only hire candidates that can implement solutions with O(nlogn) complexity.

Max Langhof

The key consideration here is that integers of the same value are indistinguishable. Thus, all you need to do is store a count of each distinct value in the container.

Then, you can just use a std::map<int, size_t> as backing structure that maps each integer (key) to the number of times it exists in your data structure (value = count).

Inserting and erasing single elements is just incrementing and decrementing (possibly removing in the latter case) values for the given key (both O(log(distinct_values_in_container)) for finding the key).

Since std::map is ordered, you can use lower_bound and upper_bound to do binary search, so finding the keys in [from, to) is very efficient (finding the range is also O(log(distinct_values_in_container))). Erasing them or summing their counts is easy then (runtime is more complicated here).


If you want to gain extra credit, it will pay to understand the limitations of asymptotic runtimes. Consider these points:

What these asymptotic runtimes mean in practice depends a lot on the usage pattern. If no duplicates are ever inserted, we are at O(n), but you can also get arbitrarily good times (in terms of n = number of insertions) if there are lots of identical elements (for example, if each key has O(exp(n)) values then O(distinct_values_in_container) = O(log(n))). In the extreme case that all involved integers are the same, all operations are O(1).

As an interviewee, I would also talk about whether these asymptotic runtimes are meaningful in practice. It may be that the map's tree structure (which is toxic for the cache and branch predictor) loses to a simple std::vector<std::pair<int, size_t>> (if erasure is always in bulk) or even a std::vector<size_t> (if the keys are "dense") for the given application.


I think your main mistake (and why you were rejected) is not realizing that there is no need to store each inserted integer separately. You unfortunately also seem to have missed the possibility of keeping the list sorted, but I don't see where the O(n^2) comes from.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What data structure could be used to store objects with multiple comparable attributes

From Dev

What is the best data structure to use when adding elements to list based on comparisons c#

From Dev

Best approach to storing scientific data sets on disk C++

From Dev

Best Data Structure for storing license plates and searching if a given license plate exists

From Dev

Meteor - What is the best process for securing forms and storing sensitive data

From Dev

What is the best data structure to store a hexagonal tilemap

From Dev

Appropriate data structure for storing the data

From Dev

Discovering the best approach to storing a specific object-oriented data structure

From Dev

What is the best data type for storing a string of 2014-2015?

From Dev

What is the best data structure to implement a queue?

From Dev

What is the Best Method for Storing Data

From Dev

What is the best data structure to store distance sensitive spatial data?

From Dev

What is the best way to destruct the structure in C

From Dev

What are the best practices related to managing keys used in git checkout for ansible?

From Dev

What fundamental data structure is used to implement NSOrderedSet

From Dev

What is the best practice for storing multi-layered data in mySQL?

From Dev

Storing any type of data in a node using void pointer(Could be int char or a structure)

From Dev

What is the best method for storing data in MySQLi with lots of columns?

From Dev

What kind of data structure will be best for storing a key-value pair where the value will be a String for some key and a List<String> for some keys?

From Dev

What is the best normalized way of storing data where titles are subject to change

From Dev

Discovering the best approach to storing a specific object-oriented data structure

From Dev

Best practice for managing most used cached images

From Dev

C# Data Structure Similar to A Thread(Storing method calls)

From Dev

C#: Best structure for storing a set of string values needed in a project?

From Dev

What is the best practice for storing multi-layered data in mySQL?

From Dev

What data structure is used in apps for modifiable lists?

From Dev

ASP.NET. What is best practices for managing sensitive data in *.config files for any non-Azure deployment

From Dev

What's the best way to design this structure in c?

From Dev

What could be best approach for regression suite for microservices, Can Scala be used for that?

Related Related

  1. 1

    What data structure could be used to store objects with multiple comparable attributes

  2. 2

    What is the best data structure to use when adding elements to list based on comparisons c#

  3. 3

    Best approach to storing scientific data sets on disk C++

  4. 4

    Best Data Structure for storing license plates and searching if a given license plate exists

  5. 5

    Meteor - What is the best process for securing forms and storing sensitive data

  6. 6

    What is the best data structure to store a hexagonal tilemap

  7. 7

    Appropriate data structure for storing the data

  8. 8

    Discovering the best approach to storing a specific object-oriented data structure

  9. 9

    What is the best data type for storing a string of 2014-2015?

  10. 10

    What is the best data structure to implement a queue?

  11. 11

    What is the Best Method for Storing Data

  12. 12

    What is the best data structure to store distance sensitive spatial data?

  13. 13

    What is the best way to destruct the structure in C

  14. 14

    What are the best practices related to managing keys used in git checkout for ansible?

  15. 15

    What fundamental data structure is used to implement NSOrderedSet

  16. 16

    What is the best practice for storing multi-layered data in mySQL?

  17. 17

    Storing any type of data in a node using void pointer(Could be int char or a structure)

  18. 18

    What is the best method for storing data in MySQLi with lots of columns?

  19. 19

    What kind of data structure will be best for storing a key-value pair where the value will be a String for some key and a List<String> for some keys?

  20. 20

    What is the best normalized way of storing data where titles are subject to change

  21. 21

    Discovering the best approach to storing a specific object-oriented data structure

  22. 22

    Best practice for managing most used cached images

  23. 23

    C# Data Structure Similar to A Thread(Storing method calls)

  24. 24

    C#: Best structure for storing a set of string values needed in a project?

  25. 25

    What is the best practice for storing multi-layered data in mySQL?

  26. 26

    What data structure is used in apps for modifiable lists?

  27. 27

    ASP.NET. What is best practices for managing sensitive data in *.config files for any non-Azure deployment

  28. 28

    What's the best way to design this structure in c?

  29. 29

    What could be best approach for regression suite for microservices, Can Scala be used for that?

HotTag

Archive