Tab-Scoped Ticket Search with Case-Insensitive Matching
StringsAuto-graded · 18 test casesLast updated
Filter support tickets by tab, then by a case-insensitive substring match on title or assignee, handling blank queries and unassigned tickets.
A help-desk dashboard shows support tickets in two tabs: unresolved and archived. Above the list is a search field. Implement the function that decides which tickets are visible for a given tab and search text.
Input
tickets: a list of dictionaries. Each ticket may contain:"id": an identifier (not used for matching)"status": a string, normally"unresolved"or"archived""title": a string"assignee": a string, orNoneif the ticket is unassigned
tab: the name of the tab currently shownsearch_text: the raw text typed into the search field
Task
Return the tickets that should be visible, as a list.
A ticket is visible when all of the following hold:
- Tab:
tabis one of"unresolved"or"archived"(compared ignoring letter case), and the ticket'sstatusequals the tab, again ignoring letter case. Iftabis not one of these two names, return an empty list. - Search: Let the query be
search_textwith leading and trailing whitespace removed.- If the query is empty, the search imposes no restriction.
- Otherwise the query must appear as a contiguous substring, ignoring letter case, either in the ticket's
titleor in itsassigneename. The substring can appear anywhere, not only at the start of a word.
Details:
- A ticket with
assigneeequal toNone(or with noassigneekey) has no assignee text to match, so only its title can match a non-empty query. - A ticket with no
titlekey is treated as having an empty title. - Whitespace inside the query is significant and is not altered. Only the ends are trimmed.
- Return the original ticket dictionaries, unmodified, in the same relative order as in
tickets. - Do not modify the input list.
Constraints
0 <= len(tickets) <= 10^4- Titles and assignee names are at most 200 characters.
search_textis at most 200 characters.- Case-insensitive comparison should use Unicode-aware case folding (Python's
str.casefold).
Examples
Example 1
Input: select_visible_tickets([{'id': 1, 'status': 'unresolved', 'title': 'Login failure', 'assignee': 'Alice'}, {'id': 2, 'status': 'unresolved', 'title': 'Billing question', 'assignee': 'Bob'}, {'id': 3, 'status': 'archived', 'title': 'Old login issue', 'assignee': 'ALICIA'}], 'unresolved', 'LOGIN')
Output: [{'id': 1, 'status': 'unresolved', 'title': 'Login failure', 'assignee': 'Alice'}]
Example 2
Input: select_visible_tickets([{'id': 1, 'status': 'unresolved', 'title': 'Cannot export report', 'assignee': None}, {'id': 2, 'status': 'unresolved', 'title': 'Password reset', 'assignee': 'Dana'}, {'id': 3, 'status': 'archived', 'title': 'Export fixed', 'assignee': 'Dana'}], 'unresolved', ' ')
Output: [{'id': 1, 'status': 'unresolved', 'title': 'Cannot export report', 'assignee': None}, {'id': 2, 'status': 'unresolved', 'title': 'Password reset', 'assignee': 'Dana'}]
Example 3
Input: select_visible_tickets([{'id': 1, 'status': 'unresolved', 'title': 'Slow dashboard', 'assignee': 'Evan'}, {'id': 2, 'status': 'archived', 'title': 'Slow import', 'assignee': 'Fiona'}], 'archived', 'fIo')
Output: [{'id': 2, 'status': 'archived', 'title': 'Slow import', 'assignee': 'Fiona'}]
Hints
Hint 1
Normalize first: compute the folded tab name and the trimmed, folded query once, outside the loop over tickets.
Hint 2
Handle the cheap rejections early: an unknown tab means an empty result, and a ticket whose folded status differs from the folded tab is skipped.
Hint 3
For the search, treat a missing or None assignee as an empty string, then check whether the folded query is a substring of the folded title or the folded assignee. An empty query should match everything in the tab.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a single filtering pass with careful normalization.
- Fold the tab name to a canonical case. If it is not
"unresolved"or"archived", nothing can be visible, so return an empty list. - Trim the search text and fold its case once. An empty result means the search is inactive.
- Walk through the tickets in order. For each one:
- Skip it if its folded status differs from the folded tab.
- If the query is empty, keep it.
- Otherwise fold the title (empty if missing) and the assignee (empty if
Noneor missing), and keep the ticket if the query is a substring of either.
- Return the kept tickets. Since we append while iterating in order, the original ordering is preserved, and the ticket dictionaries are returned as-is.
Why it is correct
- Folding both sides of every comparison makes matching independent of letter case.
- Computing the trimmed query once means leading and trailing spaces never affect results, while interior spaces still take part in the substring check.
- Treating a missing or
Noneassignee as an empty string means an unassigned ticket cannot match through the assignee, because a non-empty query is never a substring of an empty string. Such tickets can still match by title. - The tab check comes before the search check, so the search only applies within the active tab, as required.
Complexity
Let n be the number of tickets and L the maximum text length. Each ticket costs O(L) for folding and substring search (at most O(L * q) with a naive search, where q is the query length, bounded by 200). Overall time is O(n * L) for practical purposes. Space is O(n) for the output list, plus O(L) temporary strings.
Reference solution
from typing import List, Dict
def select_visible_tickets(tickets: List[Dict], tab: str, search_text: str) -> List[Dict]:
folded_tab = tab.casefold()
folded_query = search_text.strip().casefold()
if folded_tab not in ("unresolved", "archived"):
return []
visible = []
for ticket in tickets:
if ticket.get("status", "").casefold() != folded_tab:
continue
if not folded_query:
visible.append(ticket)
continue
title = str(ticket.get("title", "")).casefold()
assignee = ticket.get("assignee")
assignee_text = "" if assignee is None else str(assignee).casefold()
if folded_query in title or folded_query in assignee_text:
visible.append(ticket)
return visible