0% found this document useful (0 votes)
4 views4 pages

CODING

The document presents two coding problems: 'Two Sum' and 'Longest Substring Without Repeating Characters'. The first problem requires finding indices of two numbers in an array that sum to a target value, while the second problem involves determining the length of the longest substring without repeating characters in a given string. Each problem includes test cases and a solution in Python code.
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)
4 views4 pages

CODING

The document presents two coding problems: 'Two Sum' and 'Longest Substring Without Repeating Characters'. The first problem requires finding indices of two numbers in an array that sum to a target value, while the second problem involves determining the length of the longest substring without repeating characters in a given string. Each problem includes test cases and a solution in Python code.
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/ 4

Question 1: Two Sum 15 MARKS

Problem Statement:

You are given an array of integers nums and an integer target. Find the indices of the two numbers in
the array such that they add up to the given target.

Rules:

1. Each input will have exactly one solution.

2. You cannot use the same element twice.

3. Return the indices in any order.

Test Cases:

1. Input:

nums = [2, 7, 11, 15], target = 9

Output:

[0, 1]

Explanation:

nums[0] + nums[1] = 2 + 7 = 9

2. Input:

nums = [3, 2, 4], target = 6

Output:

[1, 2]
3. Input:

nums = [3, 3], target = 6

Output:

[0, 1]

4. Input:

nums = [1, 8, 10, 21], target = 31

Output:

[2, 3]

Solution :-

def twoSum(nums, 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 []

Question 2: Longest Substring Without Repeating Characters 10 MARKS

Problem Statement:

You are given a string s. Find the length of the longest substring in s that does not contain any repeating
characters.
Test Cases:

1. Input:

s = "abcabcbb"

Output:

Explanation:

The longest substring without repeating characters is "abc", with a length of 3.

2. Input:

s = "bbbbb"

Output:

Explanation:

The longest substring without repeating characters is "b", with a length of 1.

3. Input:

s = "pwwkew"

Output:

Explanation:

The longest substring without repeating characters is "wke", with a length of 3.


4. Input:

s = ""

Output:

Explanation:

The string is empty, so the length is 0.

Solutoin :-

def lengthOfLongestSubstring(s: str) -> int:

char_set = set()

left = 0

max_length = 0

for right in range(len(s)):

while s[right] in char_set:

char_set.remove(s[left])

left += 1

char_set.add(s[right])

max_length = max(max_length, right - left + 1)

You might also like