Assignment 1
Assignment 1
Two Sum
Easy
Topics
Companies
Hint
Given an array of integers nums and an integer target, return indices of the two
numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may
not use the same element twice.
Example 1:
Example 2:
Example 3:
Constraints:
Topics
Companies
You are given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order, and each of their nodes contains a single
digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the
number 0 itself.
Example 1:
Example 2:
Example 3:
The number of nodes in each linked list is in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list represents a number that does not have leading
zeros
Topics
Companies
Hint
Given a string s, find the length of the longest substring without duplicate
characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
Topics
Companies
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the
median of the two sorted arrays.
Example 1:
Example 2:
Constraints:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
Topics
Companies
Hint
Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Constraints:
6. Zigzag Conversion
Medium
Topics
Companies
P A H N
APLSIIG
Y I R
Write the code that will take a string and make this conversion given a number of
rows:
Example 1:
Example 2:
Example 3:
Constraints:
7. Reverse Integer
Medium
Topics
Companies
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes
the value to go outside the signed 32-bit integer range [-2 , 2 - 1], then return 0.
31 31
Assume the environment does not allow you to store 64-bit integers
(signed or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
Constraints:
Topics
Companies
then round the integer to remain in the range. Specifically, integers less than -
2 should be rounded to -2 , and integers greater than 2 - 1 should be rounded
31 31 31
to 2 - 1.
31
Example 1:
Input: s = "42"
Output: 42
Explanation:
The underlined characters are what is read in and the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
^
Step 3: "42" ("42" is read in)
^
Example 2:
Output: -42
Explanation:
Example 3:
Input: s = "1337c0d3"
Output: 1337
Explanation:
Example 4:
Input: s = "0-1"
Output: 0
Explanation:
Example 5:
Output: 0
Explanation:
Constraints:
9. Palindrome Number
Easy
Topics
Companies
Hint
Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a
palindrome.
Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
Companies
Given an input string s and a pattern p, implement regular expression matching with
support for '.' and '*' where:
The matching should cover the entire input string (not partial).
Example 1:
Example 2:
Example 3:
Constraints:
Topics
Companies
Hint
You are given an integer array height of length n. There are n vertical lines drawn such
that the two endpoints of the i line are (i, 0) and (i, height[i]).
th
Find two lines that together with the x-axis form a container, such that the container
contains the most water.
Example 1:
Example 2:
Constraints:
n == height.length
2 <= n <= 105
0 <= height[i] <= 104
12. Integer to Roman
Medium
Topics
Companies
Seven different symbols represent Roman numerals with the following values:
Symbo
Value
l
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
If the value does not start with 4 or 9, select the symbol of the maximal value
that can be subtracted from the input, append that symbol to the result,
subtract its value, and convert the remainder to a Roman numeral.
If the value starts with 4 or 9 use the subtractive form representing one
symbol subtracted from the following symbol, for example, 4 is 1 (I) less than
5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Only the following subtractive forms
are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM).
Only powers of 10 (I, X, C, M) can be appended consecutively at most 3 times
to represent multiples of 10. You cannot append 5 (V), 50 (L), or 500 (D)
multiple times. If you need to append a symbol 4 times use the subtractive
form.
Example 1:
Explanation:
Example 2:
Input: num = 58
Output: "LVIII"
Explanation:
50 = L
8 = VIII
Example 3:
Output: "MCMXCIV"
Explanation:
1000 = M
900 = CM
90 = XC
4 = IV
Constraints:
Code
Testcase
Test Result
Test Result
13. Roman to Integer
Easy
Topics
Companies
Hint
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is
written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V
+ II.
Roman numerals are usually written largest to smallest from left to right. However,
the numeral for four is not IIII. Instead, the number four is written as IV. Because the
one is before the five we subtract it making four. The same principle applies to the
number nine, which is written as IX. There are six instances where subtraction is
used:
Example 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
Topics
Companies
Write a function to find the longest common prefix string amongst an array of
strings.
Example 1:
Example 2:
Constraints:
15. 3Sum
Medium
Topics
Companies
Hint
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i !
= j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Example 2:
Example 3:
Constraints:
Topics
Companies
Given an integer array nums of length n and an integer target, find three integers
in nums such that the sum is closest to target.
Return the sum of the three integers.
You may assume that each input would have exactly one solution.
Example 1:
Example 2:
Constraints:
Topics
Companies
Given a string containing digits from 2-9 inclusive, return all possible letter
combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below.
Note that 1 does not map to any letters.
Example 1:
Example 2:
Example 3:
Constraints:
Code
Testcase
Test Result
Test Result
18. 4Sum
Medium
Topics
Companies
0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
Example 1:
Example 2:
Constraints:
Topics
Companies
Hint
Given the head of a linked list, remove the n node from the end of the list and return
th
its head.
Example 1:
Example 2:
Example 3:
Constraints:
Topics
Companies
Hint
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the
input string is valid.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Example 4:
Input: s = "([])"
Output: true
Constraints: