Implement Buffer Parsers and Generic Map Class
Company: Hudson
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
In C++, complete the following two independent implementation tasks.
1. **Sequential buffer reader**
You are given a raw byte buffer and a current pointer or offset. Implement three functions that read the next value as `char`, `int`, and `std::string` respectively. After each successful read, the pointer must advance so that the next call continues from the correct location.
Assume the binary encoding is:
- `char`: 1 byte
- `int`: 4 bytes
- `std::string`: a 4-byte length followed by that many bytes
Your functions should:
- verify that enough bytes remain before reading,
- safely convert the raw bytes into the requested type,
- advance the pointer or offset only on success,
- avoid unnecessary copying or allocations.
2. **Generic map wrapper**
Implement a template class that can operate on either `std::map<K, V>` or `std::unordered_map<K, V>`, where both `K` and `V` are restricted to `int` or `std::string`.
The class should:
- accept one of these containers in its constructor,
- support inserting key-value pairs,
- support querying a key and returning either the associated value or a clear `not found` result,
- output all stored entries in `key:value` format, one entry per line,
- handle empty containers correctly.
For `std::unordered_map`, any output order is acceptable. You may use template overloading or specialization if needed.
Quick Answer: This question evaluates proficiency in low-level C++ buffer parsing, memory and pointer management, safe binary deserialization, and template-based generic container design with type-restricted map wrappers.