Here Are 20 Challenging LeetCode Questions Commonly Asked in Data Science and Informatics Interviews
Here Are 20 Challenging LeetCode Questions Commonly Asked in Data Science and Informatics Interviews
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
for i in range(len(s)):
expand_around_center(i, i) # Odd-length palindromes
expand_around_center(i, i + 1) # Even-length palindromes
while True:
partitionX = (left + right) // 2
partitionY = (len(nums1) + len(nums2) + 1) // 2 - partitionX