Implement Buffer Parsers and Generic Map Class
Implement Buffer Parsers and Generic Map Class
This problem consists of two independent implementation tasks in C++:
1. Sequential Buffer Reader
Implement three functions to read data from a raw byte buffer starting at a given offset:
readChar(): Reads the next byte as achar.readInt(): Reads the next 4 bytes as anint.readString(): Reads a 4-byte length followed by that many bytes as astd::string.
After each successful read, the offset must be advanced to the next unread byte. The functions should:
- Verify that sufficient bytes are available in the buffer before attempting a read.
- Safely convert the raw bytes to the requested type.
- Advance the offset only upon successful reading.
- Minimize unnecessary copying and memory allocations.
2. Generic Map Wrapper
Implement a template class that acts as a wrapper for either std::map<K, V> or std::unordered_map<K, V>. The key (K) and value (V) types are restricted to int or std::string.
The wrapper class should:
- Accept an instance of
std::maporstd::unordered_mapin its constructor. - Support inserting key-value pairs.
- Support querying a key and returning the associated value or a distinct "not found" indicator.
- Output all stored entries in
key:valueformat, one entry per line. The order of output forstd::unordered_mapis not specified. - Handle empty containers gracefully.
Template overloading or specialization may be used as needed.
AI interview prep
Core dilemma
Safely and efficiently parsing binary data streams with variable-length components while managing buffer boundaries, and providing a flexible, type-constrained generic interface for map-like data structures.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement Buffer Parsers and Generic Map Class
Implement Buffer Parsers and Generic Map Class
This problem consists of two independent implementation tasks in C++:
1. Sequential Buffer Reader
Implement three functions to read data from a raw byte buffer starting at a given offset:
readChar(): Reads the next byte as achar.readInt(): Reads the next 4 bytes as anint.readString(): Reads a 4-byte length followed by that many bytes as astd::string.
After each successful read, the offset must be advanced to the next unread byte. The functions should:
- Verify that sufficient bytes are available in the buffer before attempting a read.
- Safely convert the raw bytes to the requested type.
- Advance the offset only upon successful reading.
- Minimize unnecessary copying and memory allocations.
2. Generic Map Wrapper
Implement a template class that acts as a wrapper for either std::map<K, V> or std::unordered_map<K, V>. The key (K) and value (V) types are restricted to int or std::string.
The wrapper class should:
- Accept an instance of
std::maporstd::unordered_mapin its constructor. - Support inserting key-value pairs.
- Support querying a key and returning the associated value or a distinct "not found" indicator.
- Output all stored entries in
key:valueformat, one entry per line. The order of output forstd::unordered_mapis not specified. - Handle empty containers gracefully.
Template overloading or specialization may be used as needed.