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

Coding Questions CAAS

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Coding Questions CAAS

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Coding Questions CAAS

• You are given an integer array nums and you have to return a new counts array. The counts
array has the property where counts[i] is the number of smaller elements to the right of nums[i].

Example 1:

Input: nums = [5,2,6,1]


Output: [2,1,1,0]
Explanation: To the right of 5 there are 2 smaller elements (2 and 1). To the right of 2 there is
only 1 smaller element (1). To the right of 6 there is 1 smaller element (1). To the right of 1 there
is 0 smaller element.

Example 2:

Input: nums = [-1]


Output: [0]

Example 3:

Input: nums = [-1,-1]


Output: [0,0]

Constraints:

1 <= nums.length <= 10^5

-10^4 <= nums[i] <= 10^4

• Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j <
k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.

Example 1:

Input: nums = [1,2,3,4,5] Output: true Explanation: Any triplet where i < j < k is valid.

Example 2:

Input: nums = [5,4,3,2,1] Output: false Explanation: No triplet exists.

Example 3:

Input: nums = [2,1,5,0,4,6] Output: true Explanation: The triplet (3, 4, 5) is valid because
nums[3] == 0 < nums[4] == 4 < nums[5] == 6.

Constraints:
1 <= nums.length <= 5 * 105

-231 <= nums[i] <= 231 - 1

Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space
complexity?

• Given a circularly sorted integer array, find a pair with a given sum. Assume there are no
duplicates in the array, and the rotation is in an anti-clockwise direction around an unknown
pivot.

For example,

Input: A[] = { 10, 12, 15, 3, 6, 8, 9 }, target = 18

Output: Pair found (3, 15)

Input: A[] = { 5, 8, 3, 2, 4 }, target = 12

Output: Pair found (4, 8)

Input: A[] = { 9, 15, 2, 3, 7 }, target = 20

Output: Pair not found

• Given a string s and an integer k, return the length of the longest substring of s such that the
frequency of each character in this substring is greater than or equal to k.

Example 1:

Input: s = "aaabb", k = 3 Output: 3 Explanation: The longest substring is "aaa", as 'a' is repeated
3 times.

Example 2:

Input: s = "ababbc", k = 2 Output: 5 Explanation: The longest substring is "ababb", as 'a' is


repeated 2 times and 'b' is repeated 3 times.

Constraints:

1 <= s.length <= 104

s consists of only lowercase English letters.

1 <= k <= 105

• Reverse bits of a given 32 bits unsigned integer.


Note:

Note that in some languages, such as Java, there is no unsigned integer type. In this case, both
input and output will be given as a signed integer type. They should not affect your
implementation, as the integer's internal binary representation is the same, whether it is signed
or unsigned.

In Java, the compiler represents the signed integers using 2's complement notation. Therefore,
in Example 2 above, the input represents the signed integer -3 and the output represents the
signed integer -1073741825.

Example 1:

Input: n = 00000010100101000001111010011100 Output: 964176192


(00111001011110000010100101000000) Explanation: The input binary string
00000010100101000001111010011100 represents the unsigned integer 43261596, so return
964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:

Input: n = 11111111111111111111111111111101 Output: 3221225471


(10111111111111111111111111111111) Explanation: The input binary string
11111111111111111111111111111101 represents the unsigned integer 4294967293, so return
3221225471 which its binary representation is 10111111111111111111111111111111.

Constraints:

The input must be a binary string of length 32

Follow up: If this function is called many times, how would you optimize it?

• Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water),
return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or


vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ]


Output: 1

Example 2:

Input: grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ]


Output: 3
Constraints:

m == grid.length

n == grid[i].length

1 <= m, n <= 300

grid[i][j] is '0' or '1'.

• 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.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] ==
9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6 Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6 Output: [0,1]

Constraints:

2 <= nums.length <= 104

-109 <= nums[i] <= 109

-109 <= target <= 109

Only one valid answer exists.

Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?

You might also like