You will implement solutions for two coding interview questions.
You are given an array points of 2D integer points, where each point is represented as [x, y]. The array may contain duplicates (the same [x, y] can appear multiple times).
Define:
freq(p)
= number of times point
p
appears in the array.
dist(p)
= squared Euclidean distance to the origin,
x^2 + y^2
.
Return k distinct points sorted by the following priority:
freq
descending)
dist
ascending)
x
ascending, then
y
ascending
points
: list of length
n
, each element
[x, y]
k
: integer,
1 <= k <= (# of distinct points)
k
distinct points following the ranking above.
1 <= n <= 2 * 10^5
-10^4 <= x, y <= 10^4
Design a data structure supporting the following operations:
set(key, value, timestamp)
: store the string
value
for string
key
at integer
timestamp
.
key
are inserted in
non-decreasing
order.
getClosest(key, timestamp) -> value or ""
:
key
has no stored values, return
""
.
value
whose stored timestamp is
closest
to the query
timestamp
.
If for key "a" you stored timestamps [2, 10, 15] with values ["x", "y", "z"]:
getClosest("a", 11)
returns
"y"
(10 is closest)
getClosest("a", 14)
returns
"z"
(15 is closest)
getClosest("a", 12)
returns
"y"
(10 and 15 are equally far; choose smaller timestamp)
2 * 10^5
total operations