When optimizing, watch for hidden inefficiencies: avoid O(n) per-step operations, don’t duplicate expensive work, and don’t eagerly materialize/convert data unless needed.

Apply these checks:

Example patterns (correct-by-construction + faster):

# 1) Avoid insert-at-front in recursion: use append + reverse
result = []
# ... DFS fills `result` with descendants-first
return list(reversed(result))

# 2) Compute expensive support once
candidate_counts = {}
for c in candidates:
    support = get_support(c, transactions)
    if support >= min_support:
        candidate_counts[c] = support

# 3) Check length before mapping
tokens = input().split()
if len(tokens) > MAX_SEQUENCE_LENGTH:
    ...
user_input = list(map(int, tokens))

# 4) Optional expensive simplify
def maybe_simplify(expr, *, simplify: bool = False):
    return sp.simplify(expr) if simplify else expr

# 5) Avoid redundant conversion
if not isinstance(arr, np.ndarray):
    arr = np.array(arr)