You are given four independent coding tasks. For each, describe your approach, time complexity, and handle edge cases.
Given a string s, return the length of the longest contiguous substring that contains no repeated characters.
s
(may be empty)
s = ""
?
O(n)
time
down pointersYou are given a linked structure where each node has two pointers:
class Node {
int val;
Node next; // next node in the same level
Node down; // head of a sublist (may be null)
}
Flatten the structure into a single-level list using only next pointers, in depth-first order:
down
list (recursively flattened) should appear immediately after the node, followed by the original
next
chain.
down
pointers should be set to
null
.
head: Node
head
of the flattened list
Design an in-memory data structure that maintains a dynamic set of unique items and supports:
insert(x)
: add item
x
if not present
remove(x)
: delete item
x
if present
getRandom()
: return a uniformly random item currently in the set
O(1)
time for all operations.
O(n)
, and how to achieve
O(1)
deletion.
Given an encoded string using the pattern k[encoded_string], decode it. The encoding may be nested.
Examples:
"3[a]2[bc]" -> "aaabcbc"
"3[a2[c]]" -> "accaccacc"
"12[z]" -> "zzzzzzzzzzzz"
Rules/notes:
k
is a positive integer and may have multiple digits.