Question: Deloitte NLA Placement Questions | 8th Feb Slot | Arrays and Strings | Recent Online Assessment 2026 | First Occurrence Index | Matrix Modification
0
Entering edit mode

Question 1: First Occurrence of a Character (Code Nugget 2)

Problem Statement:

Write a program to find the index of the first occurrence of a specific character ch in the given string Str.

Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings anywhere in the program, as these contribute to the standard output, and test cases will fail.

Constraints:

  1. The input must be a string and have lowercase alphabets only.
  2. 1 <= {length of  Str} <=100
  3. The index starts from zero.

Input Format:

  • The first line contains the string Str.
  • The second line of input contains the character ch to find the position.

Output Format:

  • The output contains the position of the first occurrence of a specific character ch in the given string Str.

Sample Input 1:

Eluid

u

 

Sample Output 1:

2

 

Explanation 1:

Given, Str = "Eluid". The first occurrence of a specific character ch 'u' is 2, which is printed as the output.

 


Question 2: Matrix Element Modification (Code Nugget 1)

Problem Statement:

(Derived from the provided Input/Output specification)

You are given an m \times n matrix. Your task is to target a specific element at a given row index i and column index j, and increase its value by adding a given integer P. Output the newly updated value of that specific matrix element.

Input Format:

  • The first line contains two integers, m, and n, separated by a space, where m is the number of rows and n is the number of columns in the matrix M.
  • The next m lines each contain n integers separated by spaces, representing the elements of the matrix.
  • The following line contains two integers, i and j, separated by a space, indicating the row and column indices of the matrix element to be modified.
  • The last line contains a single integer P, the value to be added to the element at A[i][j].

Output Format:

  • A single line output should display the updated value of the matrix element at position [i][j] after increasing it by P.

Sample Input 1:

3 2
3 4
16 8
21 89
1 1
500

 

Sample Output 1:

508

 

Explanation 1:

The given matrix is:

i \ j

0

1

0

3

4

1

16

8

2

21

89

The element at the position M[1][1] = 8, so adding 500 (i.e., 8 + 500) to it, it will become 508.

 

ADD COMMENTlink 25 days ago Sarthak • 10
0
Entering edit mode

Problem1 Solution

First Occurrence of a Character Solution

Topics Involved / Prerequisites

  • String Manipulation
  • Built-in Library Functions

Overview Finding the first occurrence of a character in a string is a fundamental string operation that can be handled using standard library tools. By utilizing C++'s built-in string searching methods, we can efficiently locate the target character without manually writing traversal loops. This approach guarantees minimal code complexity, avoids outputting unwanted arbitrary strings, and perfectly satisfies the problem's strict I/O constraints.

Approach

1. Reading the Input We read the string Str and the character ch from the standard input. Using cin automatically handles the whitespace separation.

2. Utilizingstd::string::find The C++ std::string class has a highly optimized find() method. When we pass our target character ch to Str.find(ch), it scans the string from left to right and immediately returns the 0-based index of its very first appearance. We then print this index directly to the output.

Code Impelementation

#include <iostream>
#include <string>

using namespace std;

int main() {
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string str;
    char ch;

    if (cin >> str >> ch) {
     
        size_t index = str.find(ch);
        if (index != string::npos) {
            cout << index << "\n";
        }
    }

    return 0;
}

Time Complexity

  • Time: O(N) - Where N is the length of the string. In the worst-case scenario, the function scans the entire string once.
  • Space: O(1) - We only store the input string and a couple of variables, requiring no additional scaling memory.

 

ADD COMMENTlink 16 days ago admin 1.9k
0
Entering edit mode

Problem2 Solution

Matrix Element Modification Solution

Topics Involved / Prerequisites

  • 2D Arrays / Vectors
  • Coordinate Indexing

Overview The problem requires us to isolate and update a single specific element within a 2D grid based on its exact row and column indices. By storing the input grid in a dynamic 2D vector, we easily maintain the structural relationship of the elements for coordinate-based access. Once the data is loaded into memory, retrieving and mathematically updating the target element becomes an instantaneous, constant-time operation.

Approach

1. Populating the Matrix First, we read the dimensions m (rows) and n (columns). We declare a 2D vector of that exact size. Using a nested loop, we read the subsequent integers from the standard input and place them into their respective row and column positions in our vector.

2. Targeted Modification After the matrix is fully built, the next inputs provide the target coordinates i and j, followed by the value P. We directly access matrix[i][j], add P to it, and output the newly computed value. Because vectors in C++ are 0-indexed and the problem's sample input demonstrates 0-based targeting (e.g., 1 1 targets the 2nd row, 2nd column), we can plug i and j in directly without adjusting them.

C++ Implementation-:

#include <iostream>
#include <vector>

using namespace std;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int m, n;

    if (!(cin >> m >> n)) return 0;


    vector<vector<int>> matrix(m, vector<int>(n));
    
    // Populate the matrix
    for (int r = 0; r < m; ++r) {
        for (int c = 0; c < n; ++c) {
            cin >> matrix[r][c];
        }
    }

   
    int i, j, p;
    cin >> i >> j;
    cin >> p;

 
    int updatedValue = matrix[i][j] + p;
    cout << updatedValue << "\n";

    return 0;
}

Time Complexity

  • Time: O(M * N) - We must read every element of the M by N matrix from the input stream exactly once. The actual addition operation takes O(1) time.
  • Space: O(M * N) - We store the entire matrix in a 2D vector to ensure we can access the specific i and j coordinates provided at the end of the input.


 

ADD COMMENTlink 16 days ago admin 1.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts