247 views
0 0 votes

What is the best-case time complexity of the following Python code snippet, assuming $N$ is the length of the list $\verb|L|$ ?

def data_check(L):
    seen = set()
    for item in L:
        if item in seen:
            return True
        seen.add(item)
    return False
    
# L is a list of items

  1. $O(N \log N)$ 
     
  2. $O(N)$ 
     
  3. $O(1)$ 
     
  4. $O(N^2)$

1 Answer

1 1 vote
The best case occurs if the first $2$ elements of the list are same then the fn returns on the second iteration hence best case time complexity of this program is $O(1)$
Answer:
Position:
Show:

Related questions

1 1 vote
2 2 answers
418
418 views
GO Classes asked Dec 17, 2025
418 views
Consider a singly linked list where the head pointer and the tail pointer are both maintained (i.e., you always have access to both the beginning and the end of the list)...
0 0 votes
1 1 answer
268
268 views
GO Classes asked Dec 17, 2025
268 views
You have an empty Queue $(Q)$ and an empty Stack $(S)$. The following sequence of operations is executed:$\mathrm{ENQUEUE}(Q, 10)$ $\mathrm{PUSH}(S, 20)$ $\mathrm{ENQUEUE...
3 3 votes
1 1 answer
353
353 views
GO Classes asked Dec 17, 2025
353 views
What will be the output of the following Python code snippet?A = [i 2 for i in range(5)] B = [(x, A[x]) for x in range(5) if x % 2 == 1] print(B [0] + B[0] ) $10$  $12$  ...
1 1 vote
1 1 answer
249
249 views
GO Classes asked Dec 17, 2025
249 views
Change the function name to $\verb|my_analyzer|$ and ask about the behavior.def my_analyzer(L): seen = set() for item in L: if item in seen: r...