Disk Defragmentation in Go
Disk fragmentation occurs when files are stored in non-contiguous blocks on a storage device, leading to slower access times. This challenge asks you to implement a simplified defragmentation algorithm in Go to rearrange file blocks to improve performance. The goal is to simulate defragmentation by reordering a list of file blocks to be contiguous, demonstrating a fundamental concept in operating system management.
Problem Description
You are given a list of integers representing the starting block addresses of file fragments on a disk. These addresses are not necessarily in order, and some blocks might be scattered across the disk. Your task is to implement a function that rearranges these block addresses into a contiguous sequence, starting from block 0.
The function should take a slice of integers (representing the block addresses) as input and return a new slice containing the reordered, contiguous block addresses. The returned slice should be sorted in ascending order and contain only unique block addresses.
Key Requirements:
- Contiguous Sequence: The output slice should represent a contiguous sequence of blocks starting from 0.
- Sorted Order: The block addresses within the output slice must be sorted in ascending order.
- Uniqueness: The output slice should contain only unique block addresses. Duplicate addresses in the input should be removed.
- Efficiency: While a highly optimized solution isn't strictly required, avoid excessively inefficient algorithms (e.g., repeated linear searches).
Expected Behavior:
The function should handle various input scenarios, including:
- Empty input slice.
- Slice with duplicate block addresses.
- Slice with block addresses out of order.
- Slice with block addresses that are not starting from 0 (these should be reordered to start from 0).
Examples
Example 1:
Input: [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation: The input contains scattered and duplicate block addresses. The function should remove duplicates, sort the addresses, and create a contiguous sequence starting from 0. The maximum address is 9, so the contiguous sequence will be from 0 to 9.
Example 2:
Input: []
Output: [0]
Explanation: An empty input slice should return a slice containing only 0, representing a single contiguous block.
Example 3:
Input: [0, 1, 2, 3, 4]
Output: [0, 1, 2, 3, 4]
Explanation: If the input is already contiguous and sorted, the function should return the same slice.
Example 4:
Input: [7, 2, 1, 0, 5, 3, 4, 6]
Output: [0, 1, 2, 3, 4, 5, 6, 7]
Explanation: The input is out of order. The function should sort the addresses and return a contiguous sequence starting from 0.
Constraints
- The input slice will contain non-negative integers.
- The maximum value in the input slice will be less than or equal to 1000.
- The length of the input slice will be between 0 and 1000.
- The function should complete within a reasonable time (e.g., less than 1 second) for the given constraints.
Notes
- Consider using a
map(orsetin other languages) to efficiently remove duplicate block addresses. - Sorting the block addresses is a crucial step. Go's built-in
sortpackage can be helpful. - The contiguous sequence should always start from block 0. The length of the sequence is determined by the maximum block address in the input.
- Think about how to determine the length of the contiguous sequence after removing duplicates and sorting. It's the maximum value + 1.
- The output slice should contain all block numbers from 0 up to and including the maximum block number found in the input.