This set of tasks evaluates algorithmic problem-solving and data-structure manipulation across arrays, strings, and linked lists, including probabilistic sampling, in-place space optimization, expression parsing, efficient search, and substring/sequence analysis while requiring explicit time and space complexity reasoning.

You may be asked one or more of the following independent coding tasks. For each task, implement an efficient algorithm and clearly state time/space complexity.
Design a class that supports:
init(weights)
:
weights[i]
is a positive integer weight for index
i
.
pickIndex() -> int
: returns an index
i
with probability
weights[i] / sum(weights)
.
Constraints/notes
Given a string s representing a valid expression containing non-negative integers, optional spaces, and operators +, -, *, / (integer division truncating toward zero), evaluate and return the result.
Constraints/notes
Given a sorted integer array nums (non-decreasing) and an integer target, return a pair [left, right] where:
left
is the first index with value
target
,
right
is the last index with value
target
,
[-1, -1]
if
target
does not exist.
Target complexity: O(log n).
Given a string s consisting only of '(' and ')', return the length of the longest contiguous substring that forms a valid parentheses sequence.
Given the head of a singly linked list and an integer x, reorder the list so that all nodes with values < x come before nodes with values >= x, preserving the original relative order within each partition.
Return the new head.
Given a string s, return the number of substrings of s that are palindromes.
A substring is a contiguous segment of the string.