The bitmap-based block allocator evaluates understanding of data structures, contiguous allocation algorithms, and low-level state management; it is commonly asked to assess efficiency, correctness under constraints and API design in the Coding & Algorithms domain and focuses on practical application of algorithms and system-level implementation.
You are implementing a simple block allocator backed by a bitmap.
M
blocks, indexed
0..M-1
.
bitmap[i] = 0
means the block is
free
;
bitmap[i] = 1
means
allocated
.
Design a class BitmapBlock with at least the following methods:
M
blocks (all free initially), or takes an initial bitmap state.
indexFind(start, n)
i >= start
such that blocks
[i, i+n-1]
are all free.
i
, or
-1
if no such contiguous range exists.
allocateFrom(start, n)
n
contiguous
free blocks, with the search beginning at
start
.
indexFind(start, n)
to locate a placement.
-1
and does not modify state.
countHoles()
bitmap = 001110011000
has holes
00
,
00
,
000
→ returns
3
.
1 <= M <= 10^6
0 <= start < M
1 <= n <= M
Write a small main() (or test harness) that constructs a BitmapBlock, calls the methods above, and prints results for a few cases.
Write a regular expression (or a function using a regex) that validates US 10-digit phone numbers with flexible formatting.
Assume the phone number contains exactly 10 digits total, but may include formatting characters:
(123)
or
123
.
-
,
, or no separator).
1234567890
,
123-456-7890
,
(123)456-7890
,
(123) 456 7890
.
Produce a regex that matches all valid forms and rejects invalid ones.