Explain how to find duplicates in a list

Finding duplicates in a list can be approached in several ways, depending on the requirements and constraints (e.g., time complexity, memory usage). Here are various methods:


1. Using a Dictionary (Efficient Approach)

This approach uses a dictionary (or a collections.Counter) to count occurrences of each element.

def find_duplicates(lst):
    element_counts = {}
    duplicates = []

    for item in lst:
        if item in element_counts:
            element_counts[item] += 1
        else:
            element_counts[item] = 1

    for item, count in element_counts.items():
        if count > 1:
            duplicates.append(item)

    return duplicates

# Example usage
lst = [1, 2, 3, 2, 4, 5, 1, 6, 1]
print(find_duplicates(lst))  # Output: [1, 2]

Time Complexity: O(n)O(n)
Space Complexity: O(n)O(n) (for the dictionary)


2. Using collections.Counter

A cleaner way to count occurrences is to use collections.Counter.

from collections import Counter

def find_duplicates(lst):
    counts = Counter(lst)
    return [item for item, count in counts.items() if count > 1]

# Example usage
lst = [1, 2, 3, 2, 4, 5, 1, 6, 1]
print(find_duplicates(lst))  # Output: [1, 2]

Time Complexity: O(n)O(n)
Space Complexity: O(n)O(n)


3. Using a Set (Efficient Memory-wise)

This approach uses a set to track seen elements and another set to store duplicates.

def find_duplicates(lst):
    seen = set()
    duplicates = set()

    for item in lst:
        if item in seen:
            duplicates.add(item)
        else:
            seen.add(item)

    return list(duplicates)

# Example usage
lst = [1, 2, 3, 2, 4, 5, 1, 6, 1]
print(find_duplicates(lst))  # Output: [1, 2]

Time Complexity: O(n)O(n)
Space Complexity: O(n)O(n) (for the sets)


4. Using Sorting (In-Place)

This approach first sorts the list, then checks adjacent elements for duplicates.

def find_duplicates(lst):
    lst.sort()  # Sort the list (in-place)
    duplicates = []

    for i in range(1, len(lst)):
        if lst[i] == lst[i - 1] and lst[i] not in duplicates:
            duplicates.append(lst[i])

    return duplicates

# Example usage
lst = [1, 2, 3, 2, 4, 5, 1, 6, 1]
print(find_duplicates(lst))  # Output: [1, 2]

Time Complexity: O(nlog⁡n)O(n \log n) (due to sorting)
Space Complexity: O(1)O(1) (in-place sorting)


5. Brute Force (Inefficient)

This naive approach compares each element to every other element in the list.

def find_duplicates(lst):
    duplicates = []

    for i in range(len(lst)):
        for j in range(i + 1, len(lst)):
            if lst[i] == lst[j] and lst[i] not in duplicates:
                duplicates.append(lst[i])

    return duplicates

# Example usage
lst = [1, 2, 3, 2, 4, 5, 1, 6, 1]
print(find_duplicates(lst))  # Output: [1, 2]

Time Complexity: O(n2)O(n^2)
Space Complexity: O(n)O(n) (for the duplicates list)


Summary of Approaches

MethodTime ComplexitySpace ComplexityBest For
Dictionary/CounterO(n)O(n)O(n)O(n)Large lists where speed matters
collections.CounterO(n)O(n)O(n)O(n)Concise, Pythonic implementations
Set-BasedO(n)O(n)O(n)O(n)Avoiding duplicates during search
SortingO(nlog⁡n)O(n \log n)O(1)O(1)Memory constraints or sorted output needed
Brute ForceO(n2)O(n^2)O(n)O(n)Small lists or educational purposes

For most practical purposes, dictionary-based or set-based approaches are preferred due to their efficiency.

What is a Python decorator? How do you use one?

A Python decorator is a way to modify or extend the behavior of a function or method without directly changing its code. It is a special kind of function that takes another function as input, adds some functionality to it, and then returns the modified function.

Decorators are often used for things like logging, authentication, timing, or modifying function outputs.


How a Decorator Works:

  1. A decorator is a function that wraps another function.
  2. It takes the original function as an argument.
  3. It returns a new function (or sometimes the same function) with the desired modifications.

Basic Syntax of a Decorator:

Using the @decorator_name syntax, you can easily apply a decorator to a function.

@decorator_name
def function_to_decorate():
    pass

The line @decorator_name is equivalent to writing:

function_to_decorate = decorator_name(function_to_decorate)

Example: Writing and Using a Decorator

Step 1: Write a Decorator

Here’s a simple decorator that adds logging:

def log_decorator(func):
    def wrapper(*args, **kwargs):
        print(f"Calling function: {func.__name__}")
        result = func(*args, **kwargs)
        print(f"Function {func.__name__} finished.")
        return result
    return wrapper

Step 2: Apply the Decorator

You can apply the decorator to a function using the @ syntax:

@log_decorator
def say_hello(name):
    print(f"Hello, {name}!")

say_hello("Alice")

Output:

Calling function: say_hello
Hello, Alice!
Function say_hello finished.

Why Use Decorators?

  1. Code Reusability: Decorators let you reuse logic across multiple functions.
  2. Clean Code: Keep the core function logic separate from additional behaviors.
  3. DRY Principle: Avoid repetition (Don’t Repeat Yourself).

A Practical Example: Timing a Function

Here’s a decorator that measures how long a function takes to run:

import time

def timer_decorator(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"Function {func.__name__} took {end_time - start_time:.2f} seconds to execute.")
        return result
    return wrapper

@timer_decorator
def compute_square(numbers):
    return [n ** 2 for n in numbers]

compute_square(range(1, 1000000))

Decorators with Arguments

If you want your decorator to take arguments, you need to add another level of nesting:

def repeat_decorator(times):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for _ in range(times):
                func(*args, **kwargs)
        return wrapper
    return decorator

@repeat_decorator(times=3)
def greet():
    print("Hello!")

greet()

Output:

Hello!
Hello!
Hello!

Decorators are an essential feature in Python that can simplify code and add powerful functionality with minimal changes! Let me know if you’d like further clarification or more examples. 😊

How would you implement Fibonacci sequence generation?

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. Here are several methods to generate the Fibonacci sequence:


1. Iterative Approach (Efficient)

def fibonacci_iterative(n):
    sequence = []
    a, b = 0, 1
    for _ in range(n):
        sequence.append(a)
        a, b = b, a + b
    return sequence

# Example usage
print(fibonacci_iterative(10))  # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

2. Recursive Approach (Less Efficient for Large n)

def fibonacci_recursive(n):
    if n <= 1:
        return n
    return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

# Generate a sequence
n = 10
sequence = [fibonacci_recursive(i) for i in range(n)]
print(sequence)  # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Note: This approach has exponential time complexity (O(2n)O(2^n)) due to repeated calculations.


3. Memoization (Efficient Recursive Approach)

from functools import lru_cache

@lru_cache(maxsize=None)
def fibonacci_memoized(n):
    if n <= 1:
        return n
    return fibonacci_memoized(n - 1) + fibonacci_memoized(n - 2)

# Generate a sequence
n = 10
sequence = [fibonacci_memoized(i) for i in range(n)]
print(sequence)  # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

4. Generator Approach

def fibonacci_generator(n):
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

# Example usage
print(list(fibonacci_generator(10)))  # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

5. Matrix Exponentiation (Advanced)

This method uses matrix exponentiation for a logarithmic time complexity solution.

import numpy as np

def fibonacci_matrix(n):
    F = np.matrix([[1, 1], [1, 0]])
    result = (F ** (n - 1))[0, 0]
    return result

# Generate a sequence
n = 10
sequence = [fibonacci_matrix(i) for i in range(1, n + 1)]
print(sequence)  # Output: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

6. Using a Formula (Binet’s Formula)

The Fibonacci number can also be computed using the golden ratio (ϕ\phi): F(n)=ϕn−(1−ϕ)n5F(n) = \frac{\phi^n – (1 – \phi)^n}{\sqrt{5}}

import math

def fibonacci_formula(n):
    phi = (1 + math.sqrt(5)) / 2
    return round((phi**n - (-1/phi)**n) / math.sqrt(5))

# Generate a sequence
n = 10
sequence = [fibonacci_formula(i) for i in range(n)]
print(sequence)  # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Best Practice

  • Use the iterative approach or generator for most practical applications.
  • Use memoization for recursive implementations if you need recursion and want to avoid repeated calculations.
  • Use matrix exponentiation or Binet’s formula for very large Fibonacci numbers.