0% found this document useful (0 votes)
22 views19 pages

Capgemini

The document describes various programming problems, including finding two indices that sum to a target, calculating the reverse degree of a string, removing duplicates from a sorted array, and finding the maximum profit from stock prices. It includes examples, constraints, and expected outputs for each problem. Additionally, it covers string manipulation tasks such as reversing words and finding the longest common prefix.

Uploaded by

vinupriyatpc
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)
22 views19 pages

Capgemini

The document describes various programming problems, including finding two indices that sum to a target, calculating the reverse degree of a string, removing duplicates from a sorted array, and finding the maximum profit from stock prices. It includes examples, constraints, and expected outputs for each problem. Additionally, it covers string manipulation tasks such as reversing words and finding the longest common prefix.

Uploaded by

vinupriyatpc
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/ 19

1.

Two Sum
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.

Reverse Degree of a String


Given a string s, calculate its reverse degree.

The reverse degree is calculated as follows:

1. For each character, multiply its position in the reversed alphabet


('a' = 26, 'b' = 25, ..., 'z' = 1) with its position in the string (1-
indexed).
2. Sum these products for all characters in the string.

Return the reverse degree of s.

Example 1:

Input: s = "abc"

Output: 148

Explanation:

Lette Index in
Index in Reversed Alphabet Product
r String
'a' 26 1 26
'b' 25 2 50
'c' 24 3 72

The reversed degree is 26 + 50 + 72 = 148.

Example 2:

Input: s = "zaza"

Output: 160

Explanation:

Lette Index in
Index in Reversed Alphabet Product
r String
'z' 1 1 1
'a' 26 2 52
'z' 1 3 3
'a' 26 4 104
The reverse degree is 1 + 52 + 3 + 104 = 160.

Constraints:

 1 <= s.length <= 1000


 s contains only lowercase English letters.

Remove Duplicates from Sorted


Array
Easy
Topics
Companies
Hint

Given an integer array nums sorted in non-decreasing order, remove


the duplicates in-place such that each unique element appears
only once. The relative order of the elements should be kept
the same. Then return the number of unique elements in nums.

Consider the number of unique elements of nums to be k, to get


accepted, you need to do the following things:

 Change the array nums such that the first k elements


of nums contain the unique elements in the order they were
present in nums initially. The remaining elements of nums are not
important as well as the size of nums.
 Return k.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array


int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

Example 1:

Input: nums = [1,1,2]


Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2
respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]


Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4
respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:

 1 <= nums.length <= 3 * 104


 -100 <= nums[i] <= 100
 nums is sorted in non-decreasing order.

Count Elements With Maximum


Frequency
Easy
Topics
Companies
Hint

You are given an array nums consisting of positive integers.

Return the total frequencies of elements in nums such that those


elements all have the maximum frequency.
The frequency of an element is the number of occurrences of that
element in the array.

Example 1:

Input: nums = [1,2,2,3,1,4]


Output: 4
Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.

Example 2:

Input: nums = [1,2,3,4,5]


Output: 5
Explanation: All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.

Constraints:

 1 <= nums.length <= 100


 1 <= nums[i] <= 100

Best Time to Buy and Sell Stock


You are given an array prices where prices[i] is the price of a given stock on
the i day.
th

You want to maximize your profit by choosing a single day to buy one
stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If
you cannot achieve any profit, return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]


Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]


Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

Constraints:

 1 <= prices.length <= 105


 0 <= prices[i] <= 104

Reverse Words in a String


Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters.


The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single


space.

Note that s may contain leading or trailing spaces or multiple spaces


between two words. The returned string should only have a single
space separating the words. Do not include any extra spaces.

Example 1:

Input: s = "the sky is blue"


Output: "blue is sky the"

Example 2:

Input: s = " hello world "


Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:
Input: s = "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed
string.

Constraints:

 1 <= s.length <= 104


 s contains English letters (upper-case and lower-case), digits, and
spaces ' '.
 There is at least one word in s.

Rotate Array
Given an integer array nums, rotate the array to the right by k steps,
where k is non-negative.

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3


Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: nums = [-1,-100,3,99], k = 2


Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Constraints:

 1 <= nums.length <= 105


 -231 <= nums[i] <= 231 - 1
 0 <= k <= 105
Fibonacci Number
The Fibonacci numbers, commonly denoted F(n) form a sequence,
called the Fibonacci sequence, such that each number is the sum of
the two preceding ones, starting from 0 and 1. That is,

F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.

Given n, calculate F(n).

Example 1:

Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Constraints:

 0 <= n <= 30

Longest Common Prefix


Write a function to find the longest common prefix string amongst an
array of strings.

If there is no common prefix, return an empty string "".


Example 1:

Input: strs = ["flower","flow","flight"]


Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]


Output: ""
Explanation: There is no common prefix among the input strings.

Constraints:

 1 <= strs.length <= 200


 0 <= strs[i].length <= 200
 strs[i] consists of only lowercase English letters if it is non-empty.

Longest Substring Without


Repeating Characters
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:

 0 <= s.length <= 5 * 104


 s consists of English letters, digits, symbols and spaces.

Valid Anagram
Given two strings s and t, return true if t is an anagram of s,
and false otherwise.

Example 1:

Input: s = "anagram", t = "nagaram"

Output: true

Example 2:

Input: s = "rat", t = "car"

Output: false

Constraints:

 1 <= s.length, t.length <= 5 * 104


 s and t consist of lowercase English letters.

Exchange Seats
 SQL Schema

 Pandas Schema

 Table: Seat
 +-------------+---------+
 | Column Name | Type |
 +-------------+---------+
 | id | int |
 | student | varchar |
 +-------------+---------+
 id is the primary key (unique value) column for this table.
 Each row of this table indicates the name and the ID of a student.
 The ID sequence always starts from 1 and increments continuously.

 Write a solution to swap the seat id of every two consecutive
students. If the number of students is odd, the id of the last
student is not swapped.
 Return the result table ordered by id in ascending order.
 The result format is in the following example.

 Example 1:
 Input:
 Seat table:
 +----+---------+
 | id | student |
 +----+---------+
 | 1 | Abbot |
 | 2 | Doris |
 | 3 | Emerson |
 | 4 | Green |
 | 5 | Jeames |
 +----+---------+
 Output:
 +----+---------+
 | id | student |
 +----+---------+
 | 1 | Doris |
 | 2 | Abbot |
 | 3 | Green |
 | 4 | Emerson |
 | 5 | Jeames |
 +----+---------+
 Explanation:
 Note that if the number of students is odd, there is no need to change the last one's seat.

Group Anagrams
Medium
Topics
Companies

Given an array of strings strs, group the anagrams together. You can
return the answer in any order.
Example 1:

Input: strs = ["eat","tea","tan","ate","nat","bat"]

Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

Explanation:

 There is no string in strs that can be rearranged to form "bat".


 The strings "nat" and "tan" are anagrams as they can be rearranged
to form each other.
 The strings "ate", "eat", and "tea" are anagrams as they can be
rearranged to form each other.

Example 2:

Input: strs = [""]

Output: [[""]]

Example 3:

Input: strs = ["a"]

Output: [["a"]]

Constraints:

 1 <= strs.length <= 104


 0 <= strs[i].length <= 100
 strs[i] consists of lowercase English letters.

Median of Two Sorted Arrays


Given two sorted arrays nums1 and nums2 of size m and n respectively,
return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).


Example 1:

Input: nums1 = [1,3], nums2 = [2]


Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]


Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Constraints:

 nums1.length == m
 nums2.length == n
 0 <= m <= 1000
 0 <= n <= 1000
 1 <= m + n <= 2000
 -106 <= nums1[i], nums2[i] <= 106

Longest Happy String


A string s is called happy if it satisfies the following conditions:

 s only contains the letters 'a', 'b', and 'c'.


 s does not contain any of "aaa", "bbb", or "ccc" as a substring.
 s contains at most a occurrences of the letter 'a'.
 s contains at most b occurrences of the letter 'b'.
 s contains at most c occurrences of the letter 'c'.

Given three integers a, b, and c, return the longest possible


happy string. If there are multiple longest happy strings, return any of
them. If there is no such string, return the empty string "".

A substring is a contiguous sequence of characters within a string.

Example 1:
Input: a = 1, b = 1, c = 7
Output: "ccaccbcc"
Explanation: "ccbccacc" would also be a correct answer.

Example 2:

Input: a = 7, b = 1, c = 0
Output: "aabaa"
Explanation: It is the only correct answer in this case.

Constraints:

 0 <= a, b, c <= 100


 a+b+c>0

Add Two Numbers


Medium
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:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:

Input: l1 = [0], l2 = [0]


Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]


Output: [8,9,9,9,0,0,0,1]

Constraints:

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

Valid Palindrome
A phrase is a palindrome if, after converting all uppercase letters into
lowercase letters and removing all non-alphanumeric characters, it
reads the same forward and backward. Alphanumeric characters
include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: s = "A man, a plan, a canal: Panama"


Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"


Output: false
Explanation: "raceacar" is not a palindrome.

Example 3:

Input: s = " "


Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.

Constraints:

 1 <= s.length <= 2 * 105


 s consists only of printable ASCII characters.

Remove Nth Node From End of List


Given the head of a linked list, remove the nth node from the end of the
list and return its head.

Example 1:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

Example 2:

Input: head = [1], n = 1


Output: []

Example 3:

Input: head = [1,2], n = 1


Output: [1]

Constraints:

 The number of nodes in the list is sz.


 1 <= sz <= 30
 0 <= Node.val <= 100
 1 <= n <= sz

Palindrome Number
Given an integer x, return true if x is a palindrome, and false otherwise.

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:

 -231 <= x <= 231 - 1

Move Zeroes
Given an integer array nums, move all 0's to the end of it while
maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the
array.

Example 1:

Input: nums = [0,1,0,3,12]


Output: [1,3,12,0,0]

Example 2:

Input: nums = [0]


Output: [0]

Constraints:

 1 <= nums.length <= 104


 -231 <= nums[i] <= 231 - 1
Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of a given stock on
the i day.
th

You want to maximize your profit by choosing a single day to buy one
stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If
you cannot achieve any profit, return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]


Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]


Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

Constraints:

 1 <= prices.length <= 105


 0 <= prices[i] <= 104

You might also like