Tuple or List? Pick the Right Tool

Easy ⏱ 8 min 79% acceptance ★★★★★ 4.5
Implement recommend_container(needs_mutation, needs_hashable) that captures the classic tuple-vs-list tradeoff. Return "list" if needs_mutation is True (lists support in-place changes; tuples don't). Otherwise, return "tuple" if needs_hashable is True (only tuples of hashable items can be dict keys or set elements). Otherwise, return "either".

Examples

Example 1
Input
recommend_container(True, False)
Output
'list'
Explanation

Mutation is required, so a list wins regardless of hashability needs.

Example 2
Input
recommend_container(False, True)
Output
'tuple'
Explanation

No mutation needed, but it must be hashable — a tuple fits.

Example 3
Input
recommend_container(False, False)
Output
'either'
Explanation

Neither constraint applies, so both containers work fine.

Constraints

  • Check needs_mutation before needs_hashable.

Topics

TuplesLists

Companies

RazorpayPayPal

Hints

Hint 1

A simple if/elif/else chain in the stated priority order solves this.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.