You work for the DMV; you have a specific, sequential way of generating new license plate numbers:
Each license plate number has 6 alphanumeric characters. The numbers always come before the letters
-
The first plate number is
000000, followed by000001... -
When you arrive at
999999, the next entry would be00000A, Followed by00001A... -
When you arrive at
99999A, the next entry is00000B, Followed by00001B... -
After following the pattern to
99999Z, the next in the sequence would be0000AA... -
When
9999AAis reached, the next in the series would be0000AB...0001AB -
When
9999ABis reached, the next in the series would be0000AC...0001AC -
When
9999AZis reached, the next in the series would be0000BA...0001BA -
When
9999ZZis reached, the next in the series would be000AAA...001AAA -
And so on untill the sequence completes with
ZZZZZZ
So the pattern overview looks a bit like this:
000000
000001
...
999999
00000A
00001A
...
99999A
00000B
00001B
...
99999Z
0000AA
0001AA
...
9999AA
0000AB
0001AB
...
9999AB
0000AC
0001AC
...
9999AZ
0000BA
0001BA
...
9999BZ
0000CA
0001CA
...
9999ZZ
000AAA
001AAA
...
999AAA
000AAB
001AAB
...
999AAZ
000ABA
...
ZZZZZZ
The goal is to write the most efficient function that can return the nth element in this sequence
Each license plate is made of 6 characters:
- Digits (
0–9) come before letters (A–Z) - Leftmost positions are digits (
0-9) - Rightmost positions are letters (
A-Z) - Digits (
0-9) are used from the left until exhausted. Then, letters (A-Z) are introduced from the right - Letters increase slower than digits
- Examples:
000000,000001,000002, ...,999999- Then
00000A,00001A, ...,99999A - Then
00000B, ...,99999Z - Then
0000AA,0001AA, ...,9999ZZ - Eventually:
000AAA,001AAA, ...,999ZZZ - Ends at:
ZZZZZZ
So, the format evolves as we run out of digits:
- Start with
6digits:000000→999999 - Then
5digits +1letter:00000A→99999Z - Then
4digits +2letters:0000AA→9999ZZ - Eventually ends at
6letters:ZZZZZZ
- Digits (
0-9) are represented as# - Letters (
A-Z) are presented as@ - Total of
7buckets' - Total of
501.363.136License Plates (From0to501.363.135)
| Format | Number of digits | Number of letters | Number of combinations |
|---|---|---|---|
###### |
6 |
0 |
10⁶ (1.000.000) × 26⁰ (1) = 1.000.000 |
#####@ |
5 |
1 |
10⁵ (100.000) × 26¹ (26) = 2.600.000 |
####@@ |
4 |
2 |
10⁴ (10.000) × 26² (676) = 6.760.000 |
###@@@ |
3 |
3 |
10³ (1.000) × 26³ (17.576) = 17.576.000 |
##@@@@ |
2 |
4 |
10² (100) × 26⁴ (456.976) = 45.697.600 |
#@@@@@ |
1 |
5 |
10¹ (10) × 26⁵ (11.881.376) = 118.813.760 |
@@@@@@ |
0 |
6 |
10⁰ (1) x 26⁶ (308.915.776) = 308.915.776 |
∅ |
∅ |
∅ |
501.363.136 |
- Digits (
0-9) are represented as# - Letters (
A-Z) are presented as@
| Format | Format Range | Index Range | Letter | Letter Bucket Format Range | Letter Bucket Index Range |
|---|---|---|---|---|---|
###### |
000000 - 999999 |
0 - 999.999 |
∅ |
∅ |
∅ |
∅ |
∅ |
∅ |
|||
∅ |
∅ |
∅ |
|||
| ... | ... | ... | |||
∅ |
∅ |
∅ |
|||
#####@ |
00000A - 99999Z |
1.000.000 - 3.599.999 |
A |
00000A - 99999A |
0 - 99.999 |
B |
00000B - 99999B |
100.000 - 199.999 |
|||
C |
00000C - 99999C |
200.000 - 299.999 |
|||
| ... | ... | ... | |||
Z |
00000Z - 99999Z |
2.500.000 - 2.599.999 |
|||
####@@ |
0000AA - 9999ZZ |
3.600.000 - 10.359.999 |
AA |
0000AA - 9999AA |
0 - 9.999 |
AB |
0000AB - 9999AB |
10.000 - 19.999 |
|||
AC |
0000AC - 9999AC |
20.000 - 29.999 |
|||
| ... | ... | ... | |||
ZZ |
0000ZZ - 9999ZZ |
6.750.000 - 6.759.999 |
|||
###@@@ |
000AAA - 999ZZZ |
|
AAA |
000AAA - 999AAA |
0 - 999 |
AAB |
000AAB - 999AAB |
1.000 - 1.999 |
|||
AAC |
000AAC - 999AAC |
2.000 - 2.999 |
|||
| ... | ... | ... | |||
ZZZ |
000ZZZ - 999ZZZ |
17.575.000 - 17.575.999 |
|||
##@@@@ |
00AAAA - 99ZZZZ |
27.936.000 - 73.633.599 |
AAAA |
00AAAA - 99AAAA |
0 - 99 |
AAAB |
00AAAB - 99AAAB |
100 - 199 |
|||
AAAC |
00AAAC - 99AAAC |
200 - 299 |
|||
| ... | ... | ... | |||
ZZZZ |
00ZZZZ - 99ZZZZ |
45.697.500 - 45.697.599 |
|||
#@@@@@ |
0AAAAA - 9ZZZZZ |
73.633.600 - 192.447.359 |
AAAAA |
0AAAAA - 9AAAAA |
0 - 9 |
AAAAB |
0AAAAB - 9AAAAB |
10 - 19 |
|||
AAAAC |
0AAAAC - 9AAAAC |
20 - 29 |
|||
| ... | ... | ... | |||
ZZZZZ |
0ZZZZZ - 9ZZZZZ |
118.813.750 - 118.813.759 |
|||
@@@@@@ |
AAAAAA - ZZZZZZ |
192.447.360 - 501.363.135 |
AAAAAA |
AAAAAA - ∅ |
0 - ∅ |
AAAAAB |
AAAAAB - ∅ |
1 - ∅ |
|||
AAAAAC |
AAAAAC - ∅ |
2 - ∅ |
|||
| ... | ... | ... | |||
ZZZZZZ |
ZZZZZZ - ∅ |
308.915.775 - ∅ |
How do we find the nth License Plate?
- Split the plate into 2 parts:
- Numeric part: the digits (
0-9) at the start - Letter part: the letters (
A-Z) at the end
- Numeric part: the digits (
- For a given letter suffix length
L:- The number of digits =
6-L(because total length is always6) - Numeric combinations =
10^(6 - L)(all possible numbers of that length) - Letter combinations =
26^L(all letter sequences of lengthL, since26letters)
- The number of digits =
- Total combinations for that letter suffix length:
total=numeric combinations×letter combinations
- Check if the target
licensePlateIndexfits in these combinations:- If
licensePlateIndex < total, the plate's letter suffix length isL - Else, subtract total from
licensePlateIndexand try the nextL+1
- If
- Calculate numeric and letter parts:
- Numeric part
index=licensePlateIndex%numeric combinations. The modulo operation (%) gives the remainder when dividing the index by the number of numeric combinations. This remainder tells us where within the current letter group the License Plate falls — that is, which exact numeric sequence (like000123) it corresponds to - Letter group
index=licensePlateIndex÷numeric combinations. Integer division tells us how many full blocks of numeric combinations fit into the index. Each block corresponds to a different letter suffix (likeA,B, ...,AA,AB, etc.). So this quotient tells us which letter suffix group the license plate belongs to
- Numeric part
- Convert these indexes to strings:
- Numeric part: zero-padded number of length
6-L - Letter part:
base-26representation of the letter group index, mapped to lettersA-Z
- Numeric part: zero-padded number of length
Why does this work? Because the license plate sequence is organized as blocks:
- Each letter suffix group contains all numeric combinations (e.g., from
000000to999999) - Dividing the overall index (License Plate index you’re looking for) by the numeric block size tells us which letter group we are in
- The remainder after that division gives the position inside that numeric block
This is essentially how numbering systems work when you have a base (like digits and letters combined): you break the number into "digits" of different bases by repeated division and modulo
- The loop iterates over letter suffix lengths from
0to6, so at most7iterations - Inside the loop, operations like exponentiation, division, modulo, and base conversions are constant time for fixed-length plates
- Overall, the time complexity is
O(1)— constant time regardless of the inputlicensePlateIndex - Answer:
O(1)
- Fixed-size constants (digits
0-9, lettersA-Z) are of constant size:10and26respectively →O(1) - Result strings (
numericChunk,letterChunk) have a maximum length of6characters →O(1) - Temporary variables (scalars,
BigNumberinstances) consume constant space →O(1) - Memory usage does not scale with input
licensePlateIndex(just likeTime Complexity), so overall space complexity isO(1) - Answer:
O(1)
Given an array of URLs and a MAX_CONCURRENCY integer, implement a function that will asynchronously fetch each URL, not requesting more than MAX_CONCURRENCY URLs at the same time. The URLs should be fetched as soon as possible. The function should return an array of responses for each URL
This exercise demonstrates how to fetch multiple image URLs concurrently, while limiting the number of simultaneous HTTP requests. It retrieves image metadata, such as buffer size and dimensions, for each URL. The function returns an array where each element corresponds to either the image metadata or an error, preserving the order of the input URLs
A queue is used to control concurrency effectively. By using a queue with a fixed number of workers (MAX_QUEUE_CONCURRENCY), we ensure that no more than the allowed number of requests run at the same time. This avoids overwhelming the network or the remote servers and helps manage resource usage efficiently. Additionally, the queue preserves task order, enabling the results array to map back to the original input order
- Each URL is fetched exactly once, and the overhead of managing the queue and callbacks scales linearly with the number of URLs. The concurrency controls do not increase the time complexity but do impact actual runtime performance by limiting simultaneous requests
- Answer:
O(n)— wherenis the number of URLs
- The space complexity is linear because the function stores a results array with one slot per URL, and each slot holds either the image metadata or an error object. Additional memory is used for each HTTP response buffer and image dimension data but does not exceed
O(n)overall - Answer:
O(n)— wherenis the number of URLs