0% found this document useful (0 votes)
139 views2 pages

Top 50 LeetCode Python Interview Questions

The document lists the top 50 LeetCode Python interview questions, starting with 'Two Sum' and 'Valid Anagram'. Each question includes a description, example input and output, along with the corresponding Python code to solve it. The examples demonstrate how to implement the solutions and verify their correctness.

Uploaded by

beautysline212
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views2 pages

Top 50 LeetCode Python Interview Questions

The document lists the top 50 LeetCode Python interview questions, starting with 'Two Sum' and 'Valid Anagram'. Each question includes a description, example input and output, along with the corresponding Python code to solve it. The examples demonstrate how to implement the solutions and verify their correctness.

Uploaded by

beautysline212
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like