Question: SparkNest Systems Coding round | Recent Online Assessment 2025 | 2D Array Generation & Anagram Logic | SparkNest Coding Round | Incremental Matrix Building & String Sorting
0
Entering edit mode

Question 1: Generate 2D Array

Problem Statement: Write a program that takes 4 inputs, where each input consists of 2 numbers in the format x,y. You are required to print a two-dimensional array having x rows and y columns for each input. The elements of the arrays should be whole numbers starting from 1 and incrementing by 1.

input-:

2,2
2,3
3,3
3,4

Sample Output:

[[1, 2], [3, 4]]
[[1, 2, 3], [4, 5, 6]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

 

Question 2: Anagram Check

Problem Statement: Determine if two given words are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Input Format:

  • The first line contains the first word.

  • The second line contains the second word.

Output Format:

  • Print 1 if the two words are anagrams.

  • Print 0 if the two words are not anagrams.

Input-: 

Creative
Reactive

Ouput-: 

1

 

0
Entering edit mode

Problem 1 Solution

Generate 2D Array

Topics Involved / Prerequisites

  • Nested Loops
  • String Formatting
  • Input Parsing

Overview

The task requires reading exactly 4 pairs of dimensions (rows and columns) separated by a comma. For each pair x,y, we must generate a 2D grid filled sequentially starting from 1. The output must strictly match a specific bracketed format [[...], [...]] which mimics how 2D arrays are printed in languages like Python.

Approach

  1. Input Parsing: Loop 4 times. Read the input as a string, find the position of the comma, and extract x and y using substring conversion.
  2. Sequential Generation: Keep a counter variable initialized to 1.
  3. Formatting: Instead of storing the numbers in an actual 2D matrix in memory, we can build and print the formatted string directly using nested loops.
    • Print the opening [.
    • For each row, print an opening [, then loop through columns printing the counter while handling the , separators correctly.
    • Print the closing ] and handle the commas between rows.

Code Implementation (C++)

#include <iostream>
#include <string>

using namespace std;

void generate2DArrays() {
    // Process exactly 4 inputs as per the problem statement
    for (int k = 0; k < 4; ++k) {
        string input;
        // Read input and break if EOF is reached unexpectedly
        if (!(cin >> input)) break; 
        
        // Parse x and y based on the comma delimiter
        int commaPos = input.find(',');
        int x = stoi(input.substr(0, commaPos));
        int y = stoi(input.substr(commaPos + 1));
        
        int val = 1;
        
        // Print the array in the required format
        cout << "[";
        for (int i = 0; i < x; ++i) {
            cout << "[";
            for (int j = 0; j < y; ++j) {
                cout << val++;
                if (j < y - 1) cout << ", "; // Comma between elements
            }
            cout << "]";
            if (i < x - 1) cout << ", ";     // Comma between rows
        }
        cout << "]\n";
    }
}

// Example usage inside main:
// int main() { generate2DArrays(); return 0; }

Time and Space Complexity

  • Time Complexity: O(x * y) per input, as we must iterate exactly x * y times to generate the sequence.
  • Space Complexity: O(1) auxiliary space, since we are directly printing the output rather than storing the matrix in memory.
ADD COMMENTlink 8 days ago admin 1.9k
0
Entering edit mode

Problem 2 Solution: 

Anagram Check

Topics Involved / Prerequisites

  • String Manipulation
  • Character Encoding (ASCII)
  • Frequency Counting / Hashing

Overview

Two strings are anagrams if they contain the exact same characters in the exact same quantities. Looking closely at your example ("Creative" and "Reactive"), the output is 1. This indicates that the comparison must be case-insensitive (since the uppercase 'C' and 'R' swap cases between the two words).

Approach

  1. Length Check: If the two strings have different lengths, they cannot be anagrams. Immediately return 0.
  2. Frequency Array: Create an integer array of size 26 (representing the alphabet) initialized to 0.
  3. Count Characters: Iterate through the strings. Convert each character to lowercase. For the first string, increment the frequency of that character. For the second string, decrement it.
  4. Validation: If the strings are valid anagrams, every single counter in the frequency array will have balanced back out to 0. If any counter is non-zero, return 0.

Code Implementation (C++)

#include <iostream>
#include <string>
#include <vector>
#include <cctype>

using namespace std;

int checkAnagram(string s1, string s2) {
    // If lengths differ, they can't be anagrams
    if (s1.length() != s2.length()) {
        return 0;
    }


    vector<int> freq(26, 0);


    for (size_t i = 0; i < s1.length(); ++i) {
        freq[tolower(s1[i]) - 'a']++;
        freq[tolower(s2[i]) - 'a']--;
    }


    for (int count : freq) {
        if (count != 0) {
            return 0;
        }
    }

    return 1;
}

Time and Space Complexity

  • Time Complexity: O(N) where N is the length of the strings. We only need to traverse the strings once.
  • Space Complexity: O(1) because the frequency array is always a constant size of 26, regardless of the input size.
ADD COMMENTlink 8 days ago admin 1.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts