Nutanix Interview Question
Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2024-08-29Added By Anonymous
Source: Link
Original Post:
Implement a class for a memory-backed sparse file.
The file can be really large (e.g., 100 GB); however, the majority (e.g., 90%) of it would be empty. Memory consumption should be very close to the amount of data stored (assume reasonably sized chunks).
Public functions required:
*WriteData(int offset, int length, char data_in):
Writes data from data_in to the file region [offset, offset + length).
ReadData(int offset, int length):
Returns char *data_out. Fills the empty portions of the range with null characters.
EraseData(int offset, int length):
Erases data in the specified range.
Note: Ownership of data_in and data_out is with the caller. data_in is not guaranteed to stay alive after the Write call.
Example:
WriteData(5, 5, "12345")
Complete the following code-
class SparseFile{
public:
void Write(int offset, int length, char* data){
}
char* Read(int offset, int length){
}
void Erase(int offset, int length){
}
};
Please let me know your approach. How can we solve this?
Edit:
I used map<char, int>, which should not be used for this problem.
Here, it should not be used because for each char (1 byte) I am using int (4 byte). So basically I am using 40 gb for 10 gb.
Implement a class for a memory-backed sparse file.
The file can be really large (e.g., 100 GB); however, the majority (e.g., 90%) of it would be empty. Memory consumption should be very close to the amount of data stored (assume reasonably sized chunks).
Public functions required:
*WriteData(int offset, int length, char data_in):
Writes data from data_in to the file region [offset, offset + length).
ReadData(int offset, int length):
Returns char *data_out. Fills the empty portions of the range with null characters.
EraseData(int offset, int length):
Erases data in the specified range.
Note: Ownership of data_in and data_out is with the caller. data_in is not guaranteed to stay alive after the Write call.
Example:
WriteData(5, 5, "12345")
Complete the following code-
class SparseFile{
public:
void Write(int offset, int length, char* data){
}
char* Read(int offset, int length){
}
void Erase(int offset, int length){
}
};
Please let me know your approach. How can we solve this?
Edit:
I used map<char, int>, which should not be used for this problem.
Here, it should not be used because for each char (1 byte) I am using int (4 byte). So basically I am using 40 gb for 10 gb.