Author Archives: Roopendra
List Comprehension Tasks in Python
List Comprehension Tasks in Python List comprehension is one of Python's most powerful and elegant features. It lets you create new lists from existing ones in a single, readable line of code. Basic Syntax # squares = print(squares) # Squares of Even Numbers even_squares = print(even_squares) # Output: Filter Strings…
Count Character Frequency in Python
Count Character Frequency in Python Counting how many times each character appears in a string is a fundamental problem with many real-world applications, from text analysis to compression algorithms. Best Solution: Using Counter from collections import Counter def char_frequency(s): return Counter(s) result = char_frequency("hello world") print(result) # Output: Counter({'l': 3,…
Check for Anagrams in Python
Check for Anagrams in Python Two strings are anagrams if they contain the same characters in the same frequency, just in a different order. For example, "listen" and "silent" are anagrams. Solution 1: Sort and Compare def is_anagram(s1, s2): return sorted(()) == sorted(()) print(is_anagram("listen", "silent")) # True print(is_anagram("hello", "world")) #…
Two Sum Problem in Python
Two Sum Problem in Python Given a list of numbers and a target value, find the indices of two numbers that add up to the target. This is one of the most popular LeetCode-style interview questions. Brute Force — O(n²) def two_sum_brute(nums, target): for i in range(len(nums)): for j in…
Remove Duplicates from a List in Python
Remove Duplicates from a List in Python Cleaning a list of duplicate values is a frequent data processing task. Python offers several clean approaches depending on whether order matters. Fastest Method: Convert to Set (Order Not Preserved) my_list = unique_list = list(set(my_list)) print(unique_list) # Output: (order may vary) Preserve Order:…
Find the Second Largest Number in a List in Python
Find the Second Largest Number in a List in Python Finding the second largest element is a common interview problem. The key challenge is handling duplicates correctly. Solution 1: Remove Duplicates, Then Sort def second_largest(nums): unique = list(set(nums)) () return unique print(second_largest()) # Output: 45 Solution 2: Using sorted() and…
Check for Prime Numbers in Python
Check for Prime Numbers in Python A prime number is a number greater than 1 that has no divisors other than 1 and itself. Efficiently checking for primes is a classic coding problem. Efficient Solution (Check up to Square Root) import math def is_prime(n): if n < 2: return False…
Find the Factorial of a Number in Python
Find the Factorial of a Number in Python The factorial of a number n (written as n!) is the product of all positive integers up to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. Using Recursion def factorial_recursive(n): if n == 0…
Check for Palindrome in Python
Check for Palindrome in Python A palindrome is a string that reads the same forwards and backwards, such as "racecar" or "madam". This problem builds directly on string reversal. Simple Solution def is_palindrome(s): return s == s # Examples print(is_palindrome("racecar")) # True print(is_palindrome("hello")) # False Case-Insensitive Check def is_palindrome_ci(s): s…
Reverse a String in Python
Reverse a String in Python One of the most common Python interview questions is reversing a string. Python makes this elegant with slicing. The Pythonic Way def reverse_string(s): return s # Example print(reverse_string("hello")) # Output: "olleh" How It Works The slicing syntax s means: start from the end, go to…