Lakshmi Narayana

Lakshmi Narayana

  • 2.1k
  • 2
  • 256

Hi Team i got the below question as assignment in interview! need help

Nov 27 2021 5:33 AM

/// Implement a `VectorMap<T>` container class which uses a `std::vector` as backing store and has the below interface.
/// Implementation should consider correctness, readability and performance.
/// Please document your impl using comments where sensible, e.g. motivation for non-obvious desing decisions / trade-offs.
/// Prefer re-using functionality of the standard library / STL where sensible.

/// NOTE: there is no single correct solution! If you can convincingly justify your approach, than that's good!

/// Please also provide a couple of sensible unit tests verifying the correctness of your impl.
/// The tests can be either implemented using simple functions (e.g. `test_insert_multiple_values()`) containing some assert() statements
/// or using one of the popular frameworks (e.g. Catch2 or Google Test).

///
/// K type of key
/// T type of value

template <typename K, typename T>
class VectorMap
{
public:
    ///
    /// Returns a reference to the mapped value of the element with key equivalent to key. If no such element exists, an exception of type std::out_of_range is thrown.
    ///
    T& at( const Key& key );

    ///
    /// Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.
    ///
    T& operator[]( const Key& key );

    ///
    /// Removes the element (if one exists) with the key equivalent to key.
    /// Returns: Number of elements removed (0 or 1).
    ///
    size_type erase( const Key& key );

    ///
    /// Checks if the container has no elements.
    ///
    bool empty() const;

    ///
    /// Attempts to extract ("splice") each element in source and insert it into *this using the comparison object of *this.
    /// If there is an element in *this with key equivalent to the key of an element from source, then that element is not extracted from source.
    ///
    void merge(VectorMap<K,T>& source);

private:
    std::vector< /* ??? */ > elems;
};