0% found this document useful (0 votes)
141 views

Here Are 20 Challenging LeetCode Questions Commonly Asked in Data Science and Informatics Interviews

Uploaded by

dzedziphilly
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)
141 views

Here Are 20 Challenging LeetCode Questions Commonly Asked in Data Science and Informatics Interviews

Uploaded by

dzedziphilly
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
You are on page 1/ 5

Here are 20 challenging LeetCode questions commonly asked in data science

and informatics interviews, along with detailed solutions:


1. Two Sum:
python
Copy
def twoSum(nums, target):
"""
Given an array of integers nums and an integer target, return indices of the two
numbers such that they add up to target.
"""
num_dict = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_dict:
return [num_dict[complement], i]
num_dict[num] = i
return []
2. Longest Substring Without Repeating Characters:
python
Copy
def lengthOfLongestSubstring(s):
"""
Given a string s, find the length of the longest substring without repeating
characters.
"""
char_index = {}
max_length = 0
start = 0
for i, char in enumerate(s):
if char in char_index and char_index[char] >= start:
start = char_index[char] + 1
char_index[char] = i
max_length = max(max_length, i - start + 1)
return max_length
3. Merge Two Sorted Lists:
python
Copy
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

def mergeTwoLists(l1, l2):


"""
Merge two sorted linked lists and return it as a sorted list.
"""
dummy = ListNode()
curr = dummy
while l1 and l2:
if l1.val < l2.val:
curr.next = l1
l1 = l1.next
else:
curr.next = l2
l2 = l2.next
curr = curr.next
if l1:
curr.next = l1
if l2:
curr.next = l2
return dummy.next
4. Valid Parentheses:
python
Copy
def isValid(s):
"""
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
"""
stack = []
mapping = {")": "(", "}": "{", "]": "["}
for char in s:
if char in mapping:
top_element = stack.pop() if stack else '#'
if top_element != mapping[char]:
return False
else:
stack.append(char)
return not stack
5. Reverse Linked List:
python
Copy
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

def reverseList(head):
"""
Given the head of a singly linked list, reverse the list and return the reversed
list.
"""
prev = None
curr = head
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
return prev
6. Merge Intervals:
python
Copy
def merge(intervals):
"""
Given an array of intervals where intervals[i] = [start, end], merge all
overlapping intervals and return an array of the merged intervals.
"""
intervals.sort(key=lambda x: x[0])
merged = []
for interval in intervals:
if not merged or interval[0] > merged[-1][1]:
merged.append(interval)
else:
merged[-1][1] = max(merged[-1][1], interval[1])
return merged
7. Maximum Subarray:
python
Copy
def maxSubArray(nums):
"""
Given an integer array nums, find the contiguous subarray (containing at least
one number) which has the largest sum and return its sum.
"""
max_sum = nums[0]
curr_sum = nums[0]
for i in range(1, len(nums)):
curr_sum = max(nums[i], curr_sum + nums[i])
max_sum = max(max_sum, curr_sum)
return max_sum
8. Climbing Stairs:
python
Copy
def climbStairs(n):
"""
You are climbing a staircase. It takes n steps to reach the top. Each time you
can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
"""
if n <= 2:
return n
dp = [0] * (n + 1)
dp[1] = 1
dp[2] = 2
for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]
9. Longest Palindromic Substring:
python
Copy
def longestPalindrome(s):
"""
Given a string s, return the longest palindromic substring in s.
"""
if not s:
return ""

start = 0
max_length = 1

def expand_around_center(left, right):


nonlocal start, max_length
while left >= 0 and right < len(s) and s[left] == s[right]:
if right - left + 1 > max_length:
start = left
max_length = right - left + 1
left -= 1
right += 1

for i in range(len(s)):
expand_around_center(i, i) # Odd-length palindromes
expand_around_center(i, i + 1) # Even-length palindromes

return s[start:start + max_length]


10.Median of Two Sorted Arrays:
python
Copy
def findMedianSortedArrays(nums1, nums2):
"""
Given two sorted arrays nums1 and nums2, find the median of the two sorted
arrays.
"""
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1

left, right = 0, len(nums1) - 1

while True:
partitionX = (left + right) // 2
partitionY = (len(nums1) + len(nums2) + 1) // 2 - partitionX

maxLeftX = nums1[partitionX] if partitionX >= 0 else float('-inf')


minRightX = nums1[partitionX + 1] if partitionX < len(nums1) - 1 else
float('inf')

maxLeftY = nums2[partitionY] if partitionY >= 0 else float('-inf')


minRightY = nums2[partitionY + 1] if partitionY < len(nums2) - 1 else
float('inf')

if maxLeftX <= minRightY and maxLeftY <= minRightX:


if (len(nums1) + len(nums2)) % 2 == 0:
return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2
else:
return max(maxLeftX, maxLeftY)
elif maxLeftX > minRightY:
right = partitionX - 1
else:
left = partitionX + 1
These solutions cover a wide range of data structures and algorithms,
including arrays, linked lists, strings, dynamic programming, and binary search.
They demonstrate effective problem-solving strategies, efficient time and
space complexities, and clean code organization. Let me know if you have any
other questions!

You might also like