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

Hands-On_Arrays and Functions - Questions

The document outlines a hands-on SDE readiness training session focused on arrays and functions, scheduled for February 14, 2025. It includes a series of 18 programming problems of varying difficulty, primarily categorized as easy, covering topics such as array manipulation, matrix operations, and algorithm design. Each problem is accompanied by sample inputs and outputs to guide participants in their coding exercises.

Uploaded by

k.s.yogeswar3
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 views

Hands-On_Arrays and Functions - Questions

The document outlines a hands-on SDE readiness training session focused on arrays and functions, scheduled for February 14, 2025. It includes a series of 18 programming problems of varying difficulty, primarily categorized as easy, covering topics such as array manipulation, matrix operations, and algorithm design. Each problem is accompanied by sample inputs and outputs to guide participants in their coding exercises.

Uploaded by

k.s.yogeswar3
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/ 9

HANDS-ON

SDE Readiness Training

Hands-on No. : 3a

Topic : Arrays and Functions

Date : 14.02.2025

Solve the following problems

Question
Question Detail Level
No.
1 There is long queue in the billing counter of a supermarket. Tell Easy
the position of the specific customer if the names are the input.
If not found, print -1.
Sample Input: 5, [Smith Tim Eve John Dora], Eve
Sample Output: 3
Sample Input: 5, [Smith Tim Eve John Dora], Mike
Sample Output: -1

2 Given an array of size N-1 such that it only contains distinct Easy
integers in the range of 1 to N. Find the missing element.
Assume that integer range is correctly given.
Sample Input: 5, [1,2,3,5]
Sample Output: 4
Sample Input: 10, [6,1,2,8,3,4,7,10,5]
Sample Output: 9

3 Write a program to find & remove duplicate elements in the array Easy
and reprint.
Sample Input: 1 2 8 3 4 5 5 6 7 8
Sample Output: 1 2 8 3 4 5 6 7

4 Write a program to find pair of elements in the array having sum Easy
of 10. If not found any, return -1.
Sample Input: 1 2 8 3
Sample Output: (2,8)
Sample Input: 1 2 3 4 5
Sample Output: -1

5 Write a Java program to replace each element of the array with Easy
product of all other elements in a given array of integers.
Sample Input: 4,[1 2 3 4]
Sample Output: 24 12 8 6

It is going to be hard but, hard does not mean


impossible.
1
HANDS-ON
SDE Readiness Training
6 Write a Java program to accept N numbers from console and Easy
print the sum of the elements of the array with the following
condition. Condition: If the array has elements ‘a’ and ‘b’ in
succeeding order, ignore the numbers between ‘a’ and ‘b’
inclusive for the calculation.
Sample Input: [10,3,6,1,2,7,9], 6, 7
Sample Output: 22
Sample Input: [7,1,2,3,6], 6, 7
Sample Output: 19
Sample Input: [1,6,7,9], 6, 7
Sample Output: 10

7 Develop a Java program that constructs a jagged array for Easy


recording basketball tournament scores per player. The program
initiates by requesting the user to input the count of teams along
with the number of players within each team. Following this, it
enables the user to input the scores for every player across each
team. Lastly, the program computes and exhibits the average

score for every team.

8 You're developing a student grading system where each student Easy


has various assignments, each with its own weightage. Create a
program that first asks the user to input the total number of
students and the number of assignments. Then, enable the user
to input grades for each assignment for every student. Finally,
compute and present the weighted average grade for each
student, factoring in the assignment weights.

9 Given a square matrix mat[][], turn it by 90 degrees in an Easy


anticlockwise direction without using any extra space
Examples:
Input: mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: {{3, 6, 9},
{2, 5, 8},
{1, 4, 7,}}
Input: mat[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11,12}

It is going to be hard but, hard does not mean


impossible.
2
HANDS-ON
SDE Readiness Training
{13, 14, 15, 16}}
Output: {{4, 8, 12, 16},
{3, 7, 11, 15},
{2, 6, 10, 14},
{1, 5, 9, 13}}

10 Happy Number Easy


Problem statement: Write an algorithm to determine if a
number n is happy.
A happy number is a number defined by the following process:
● Starting with any positive integer, replace the number by the
sum of the squares of its digits.
● Repeat the process until the number equals 1 (where it will
stay), or it loops endlessly in a cycle which does not include
1.
● Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
Constraints:1 <= n <= 2^31 - 1
11 Encrypt the given number Easy
Problem statement: Micheal has created his own encryption
technique to encrypt a number. He makes use of the logic
behind factorial. In a factorial, we multiply the number by its
previous number and so on but if we want to encrypt a number
we don’t multiply in every step like in the case of factorial but
multiply, divide, add and subtract and repeat in the same order.

It is going to be hard but, hard does not mean


impossible.
3
HANDS-ON
SDE Readiness Training
So your task is to find the encrypted form of a number using the
Micheal’s encryption technique and you were being provided
with the number.
Sample Input 1:
2
5
8
Sample Output 1:
7
9
Explanation For Sample Input 1:
Test Case 1:
For the first test case, given number is ‘5’ so using the
encryption technique we follow the steps: ( 5 * 4 / 3 + 2 - 1 ) = 7

Test Case 2:
For the first test case, the given number is ‘8’ so using the
encryption technique we follow the steps: ( 8 * 7 / 6 + 5 - 4 * 3
/2-1)=9
Sample Input 2:
1
12
Sample Output 2:
13

12 Count of Matches in Tournament Easy


Problem Statement: You are given an integer n, the number of
teams in a tournament that has strange rules:
 If the current number of teams is even, each team gets
paired with another team. A total of n / 2 matches are
played, and n / 2 teams advance to the next round.
 If the current number of teams is odd, one team
randomly advances in the tournament, and the rest gets
paired. A total of (n - 1) / 2 matches are played, and (n -
1) / 2 + 1 teams advance to the next round.
Return the number of matches played in the tournament until a
winner is decided.

It is going to be hard but, hard does not mean


impossible.
4
HANDS-ON
SDE Readiness Training
Example 1:
Input: n = 7
Output: 6
Explanation: Details of the tournament:
- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.
- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.
- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared
the winner.
Total number of matches = 3 + 2 + 1 = 6.
Example 2:
Input: n = 14
Output: 13
Explanation: Details of the tournament:
- 1st Round: Teams = 14, Matches = 7, and 7 teams advance.
- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance.
- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance.
- 4th Round: Teams = 2, Matches = 1, and 1 team is declared
the winner.
Total number of matches = 7 + 3 + 2 + 1 = 13.
Constraints:
1 <= n <= 200

13 Two Sum (Two pointer) Easy


Problem Statement : 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 = [1, 4, 6, 8, 10], target = 12
Output: [1, 3]

It is going to be hard but, hard does not mean


impossible.
5
HANDS-ON
SDE Readiness Training
Example 3:
Input: nums = [3, 5, 7, 9, 12], target = 14
Output: [1, 3]

Constraints:
2 <= nums.length <= 10^4
-10^9 <= nums[i] <= 10^9
-10^9 <= target <= 10^9

14 Maximum Sum of Subarray of size K (Sliding Window) Easy


Problem Statement : Given an array of integers and a number
k, find the maximum sum of a subarray of size k.

Examples:
Input : arr[] = {100, 200, 300, 400}, k = 2
Output : 700
Explanation: arr3 + arr4 = 700, which is maximum.

Input : arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20}, k = 4


Output : 39
Explanation: We get maximum sum by adding subarray {4, 2,
10, 23} of size 4.

Input : arr[] = {2, 3}, k = 3


Output : Invalid
Explanation: There is no subarray of size 3 as size of whole array
is 2.
Constraints:
1 <= arr.size() <= 10^6
1 <= arr[i]<= 10^6
1 <= k<= arr.size()

15 Smallest subarray with sum greater than x (Sliding Easy


window)
Problem Statement : Given a number x and an array of
integers arr, find the smallest subarray with sum greater than

It is going to be hard but, hard does not mean


impossible.
6
HANDS-ON
SDE Readiness Training
the given value. If such a subarray do not exist return 0 in that
case.

Examples:
Input: x = 51, arr[] = [1, 4, 45, 6, 0, 19]
Output: 3
Explanation: Minimum length subarray is [4, 45, 6]
Input: x = 100, arr[] = [1, 10, 5, 2, 7]
Output: 0
Explanation: No subarray exist
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ arr.size, x ≤ 10^5
0 ≤ arr[] ≤ 10^4

16 Defuse the Bomb (Sliding window) Easy


Problem Statement : You have a bomb to defuse, and your
time is running out! Your informer will provide you with a circular
array code of length of n and a key k.
To decrypt the code, you must replace every number. All the
numbers are replaced simultaneously.
 If k > 0, replace the ith number with the sum of the next k
numbers.
 If k < 0, replace the ith number with the sum of the
previous k numbers.
 If k == 0, replace the ith number with 0.
As code is circular, the next element of code[n-1] is code[0], and
the previous element of code[0] is code[n-1].
Given the circular array code and an integer key k, return the
decrypted code to defuse the bomb!
Example 1:
Input: code = [5,7,1,4], k = 3
Output: [12,10,16,13]

It is going to be hard but, hard does not mean


impossible.
7
HANDS-ON
SDE Readiness Training
Explanation: Each number is replaced by the sum of the next 3
numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7,
5+7+1]. Notice that the numbers wrap around.
Example 2:
Input: code = [1,2,3,4], k = 0
Output: [0,0,0,0]
Explanation: When k is zero, the numbers are replaced by 0.
Example 3:
Input: code = [2,4,9,3], k = -2
Output: [12,5,6,13]
Explanation: The decrypted code is [3+9, 2+3, 4+2, 9+4].
Notice that the numbers wrap around again. If k is negative, the
sum is of the previous numbers.

Constraints:
n == code.length
1 <= n <= 100
1 <= code[i] <= 100
-(n - 1) <= k <= n – 1

17 Running Sum of 1D array (Prefix Sum) Easy


Problem Statement : Given an array nums. We define a
running sum of an array as runningSum[i] = sum(nums[0]…
nums[i]).
Return the running sum of nums.

Example 1:
Input: nums = [1,2,3,4] Output:
[1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2,
1+2+3, 1+2+3+4].
Example 2:
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1,
1+1+1, 1+1+1+1, 1+1+1+1+1].
Example 3:
Input: nums = [3,1,2,10,1]

It is going to be hard but, hard does not mean


impossible.
8
HANDS-ON
SDE Readiness Training
Output: [3,4,6,16,17]

Constraints:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6

18 Matrix Block Sum (Prefix Sum) Medium


Problem Statement : Given a m x n matrix mat and an integer
k, return a matrix answer where each answer[i][j] is the sum of
all elements mat[r][c] for:

 i - k <= r <= i + k,
 j - k <= c <= j + k, and
 (r, c) is a valid position in the matrix.

Example 1:

Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1

Output: [[12,21,16],[27,45,33],[24,39,28]]

Example 2:

Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2

Output: [[45,45,45],[45,45,45],[45,45,45]]

Constraints:

m == mat.length

n == mat[i].length

1 <= m, n, k <= 100

1 <= mat[i][j] <= 100

It is going to be hard but, hard does not mean


impossible.
9

You might also like