Top 50 LeetCode Python Interview
Questions with Output
1. Two Sum
**Question:**
Given an array of integers nums and an integer target, return indices of the two
numbers such that they add up to target.
**Example:**
Input: nums = [2,7,11,15], target = 9
Output: [0, 1]
**Python Code:**
def twoSum(nums, target):
hashmap = {}
for i, num in enumerate(nums):
diff = target - num
if diff in hashmap:
return [hashmap[diff], i]
hashmap[num] = i
# Test case
nums = [2, 7, 11, 15]
target = 9
print("Two Sum:", twoSum(nums, target))
**Expected Output:**
Two Sum: [0, 1]
2. Valid Anagram
**Question:**
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
**Example:**
Input: s = 'anagram', t = 'nagaram'
Output: True
**Python Code:**
def isAnagram(s, t):
return sorted(s) == sorted(t)
# Test case
s = "anagram"
t = "nagaram"
print("Valid Anagram:", isAnagram(s, t))
**Expected Output:**
Valid Anagram: True