Posts

Showing posts from September 1, 2024

Java Program to Expand Letters Based on Following Digits in a String- Interview Question asked in Deloitte, Infomerica

Image
The provided Java code snippet expands a compressed string based on the rules defined by the input format. Let's go through the logic step-by-step: Purpose of the Code: The code aims to decode a string where a letter is followed by a digit that specifies how many times the letter should be repeated. For example, given the input string `"d2r3w4t2"`, the output will be `"ddrrrwwwwtt"`. ### Key Components of the Code: 1. **Initialization:** - `str`: The input string to be decoded (`"d2r3w4t2"`). - `StringBuilder sb`: A dynamic string object to build the decoded output. - `char[] ch`: An array of characters from the input string to facilitate character-by-character processing. - `int i`: A loop counter initialized to zero, used to track the current character index. 2. **Main Loop (`for` loop):** - The loop iterates through each character in the `ch` array except the last one (`j - **If the current character `ch[j]` is a ...

Java Program to print the count of Consecutive character- Interview Question Asked in Mobikwik company

Image
The provided Java program counts consecutive occurrences of each character in a given string and prints the count for each sequence of consecutive characters. Let’s go through the code step-by-step to understand how it works. Program Title: "Java Program to Count Consecutive Characters in a String" Code Explanation Detailed Step-by-Step Explanation: 1. Initialization: - The program starts by defining a string `name` containing the input `"DDeee eepppaapekkrkka"`. - The string is converted to a character array `ch` using `name.toCharArray()` for easier manipulation. - `len` stores the length of the character array. - A `StringBuilder sb` is initialized to keep track of consecutive characters as they are identified. 2. Iterate Over Characters: - The main loop iterates over each character in the array `ch` using the index `j` from `0` to `len - 2` (the second last character). 3. Checking for Consecutive Characters: - The...

Java Program to Reverse Characters in a String While Preserving Space Positions Interview Question

Image
This question Asked by TechMahindra, LTIMindTree, Wipro Explanation of the Logic The given Java program reverses the characters of a string while keeping the spaces in their original positions. Here’s a step-by-step breakdown of how the program works: Step-by-Step Breakdown: Input String and Initialization: The program starts by initializing a string str with the value "i am pocket". It then converts this string into a character array c using str.toCharArray(). This allows individual manipulation of each character in the string. Another character array ch of the same length as str is created to store the result. Initially, this array is empty. Preserve Spaces in the Result Array: The first for loop iterates through the character array c. If a space character (' ') is found in c, it is directly copied to the same position in the ch array. This step ensures that all spaces from the original string str are preserved in their original positions in the ch a...