0% found this document useful (0 votes)
32 views2 pages

Coding Problems and Solutions Guide

The document presents multiple programming problems that involve string manipulation, arithmetic operations, and matrix traversal. Key tasks include moving hashes to the front of a string, counting borrow operations in subtraction, replacing characters in a string, compressing repeated characters, and traversing a 2D matrix in spiral order. Each problem is accompanied by example inputs and expected outputs for clarity.

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)
32 views2 pages

Coding Problems and Solutions Guide

The document presents multiple programming problems that involve string manipulation, arithmetic operations, and matrix traversal. Key tasks include moving hashes to the front of a string, counting borrow operations in subtraction, replacing characters in a string, compressing repeated characters, and traversing a 2D matrix in spiral order. Each problem is accompanied by example inputs and expected outputs for clarity.

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

Problem Statement : You have write a function that accepts, a string which length

is “len”, the string has some “#”, in it you have to move all the hashes to the front
of the string and return the whole string back and
print it.

char* moveHash(char str[],int n);

Example :

Sample Test Case

Input:
Move#Hash#to#Front

Output:
###MoveHashtoFront
Problem statement : You have two numbers number1 and number2, your job is to
check the number of borrow operations needed for subtraction of number1 from
number2. If the subtraction is not possible
then return the string not possible.

Example :

754
658

Answer :
2
654
666

Answer:
Not possible
Problem statement : You’re given a function that accepts the following, a string1,
its length and a character c. Your job is to replace all the occurrences of character c
in string1 and capitalize it or decapitalize it based on the character c.

Example 1
Example 2
Input :

hello world
l

Output :

heLLo worLd

Problem statement : You’re given a string where multiple characters are repeated
consecutively. You’re supposed to reduce the size of this string using mathematical
logic given as in the example below :

Example
Input :
abbccccc

Output:
ab2c5

Problem statement : You will be given a 2d matrix. Write the code to traverse the
matrix in a spiral format. Check the input and output for better understanding.

Example :

Input :

54
1234
5678
9 10 11 12
13 14 15 16
17 18 19 20

Output :

1 2 3 4 8 12 16 20 19 18 17 13 9 5 6 7 11 15 12 14 10

Common questions

Powered by AI

The challenge lies in identifying whether a borrow is necessary, which depends on each digit pair starting from the least significant digit. If the subtracted digit is larger, borrowing from the next significant digit is required. Additionally, if the first number is greater than the second, subtraction isn't possible, requiring a check and outputting 'not possible' .

To replace and change the case of character 'c', iterate through the string and each time 'c' is found, replace it with its upper or lower case version based on a specific criterion (e.g., current case). Collect and return this transformed string .

The subtraction would return 'not possible' when the minuend (number1) is greater than the subtrahend (number2), such that subtraction cannot occur without resulting in a negative outcome for digit positions. This is an error condition for borrow operations .

Applying mathematical logic enables significant reduction in string size by storing character sequences compactly, which enhances storage efficiency and speeds up processing, especially for strings with large sequences of repeated characters. It optimizes both time and space complexities .

Changing the case of specific characters can increase text data readability by distinguishing certain elements, aiding in emphasizing or categorizing content. It allows for visually separating identifiers or commands in a dataset, leading to better human readability and parsing .

To move all '#' characters to the front of a string, iterate through the string to count the number of hashes. Construct a new string starting with all the '#' characters followed by the non-hash characters in their original order .

To compress a string, traverse it to identify consecutive repeating characters. Replace each character sequence with the character followed by its repetition count. For example, for the sequence 'ccccc', replace it with 'c5' .

Efficient traversal involves maintaining and updating boundary markers (e.g., top, bottom, left, right) and conditions to check if these boundaries have shifted inward. This logic avoids revisiting traversed cells and ensures a seamless inward spiral through systematic decrementing and incrementing of boundary index values .

Spiral order traversal involves looping through the matrix starting at the top left and completing full perimeter circles inward. Keeping track of boundaries (top, right, bottom, left) is complex, requiring conditional checks to prevent re-traversal and ensure inward looping is correctly handled .

One strategy is to simulate the subtraction process manually, iterating from least to most significant digit and incrementing a counter each time borrowing from a higher place is necessary. This involves backtracking for each digit needing borrow operations and counting these incidences .

You might also like