Back to community

AtlassianEngineer of staff

  • Happy Architect HKdv
  • 241 views
2 rounds5 questions
  1. Data structures

    August 2026

    Step-By-Step Implementation: Change the given number into a binary and store it in string s. Run loop n times. Run another loop for string length s to convert 0 to “01” and 1 to “10” and store in a temporary string s1. After completion of each iteration, assign string s1 to s. Finally, return the value of the kth index in string s.

    2 questions asked

    1. Question 1

      The idea is to avoid generating the full binary string after each iteration, since its size doubles every time. Instead, we identify the original bit in m that gives rise to the k-th character after n iterations. The key observation is that the number of bit flips in a path from root to k-th character is equal to the number of set bits in offset, and parity of this count decides the final bit.

      Answer 1

      // C# Code to find kth character after // n iterations using System; class GfG { // Function to count number of set // bits (used to compute parity) static int countSetBits(int x) { int cnt = 0; while (x > 0) { cnt += x & 1; x >>= 1; } return cnt; } // Function to find the k-th character // after n iterations static char KthCharacter(int m, int n, int k) { int[] binary = new int[32]; for (int i = 0; i < 32; i++) { binary[i] = (m >> i) & 1; } int totalBits = 0; // Count number of bits in m (from MSB) for (int i = 31; i >= 0; i--) { if (binary[i] == 1) { totalBits = i + 1; break; } } int blockLength = 1 << n; int blockIndex = (k - 1) / blockLength; int offset = (k - 1) % blockLength; // If m has fewer bits than blockIndex, bit is 0 int root = (blockIndex < totalBits) ? binary[totalBits - blockIndex - 1] : 0; int flips = countSetBits(offset); // Flip if parity is odd if (flips % 2 == 0) { return root == 1 ? '1' : '0'; } else { return root == 1 ? '0' : '1'; } } static void Main() { int m = 5, n = 2, k = 5; Console.WriteLine(KthCharacter(m, n, k)); } }

    2. Question 2

      Given a stream of integers represented as arr[]. For each index i from 0 to n-1, print the multiplication of the largest, second largest, and third largest element of the subarray arr[0...i]. If i < 2 print -1.

      Answer 2

      Steps to implement the above idea: Initialize a max-heap to track the largest elements and a result list for storing outputs. Traverse the input array and push each element into the max-heap. If the heap size is less than 3, append -1 to the result list. When the heap size is at least 3, extract the three largest elements from the heap. Calculate the product of these three elements, append it to the result list, and push them back into the heap. Finally, return or print the result list containing the outputs.

  2. Round 2

    August 2026

    The idea is to use two nested loops to explore all the possible ways to buy and sell stock. The outer loop decides the day to buy the stock and the inner loop decides the day to sell the stock. The maximum difference between the selling price and buying price between every pair of days will be our answer.

    3 questions asked

    1. Question 1

      Stock Buy and Sell - Max one Transaction Allowed

      Answer 1

      using System; using System.Collections.Generic; class GFG { static int maxProfit(int[] prices) { int n = prices.Length; int res = 0; // Explore all possible ways to buy and sell stock for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { res = Math.Max(res, prices[j] - prices[i]); } } return res; } static void Main() { int[] prices = { 7, 10, 1, 3, 6, 9, 2 }; Console.WriteLine(maxProfit(prices)); } }

    2. Question 2

      Given a decimal number m. Consider its binary representation string and apply n iterations. In each iteration, replace the character 0 with the string 01, and 1 with 10. Find the kth (1-based indexing) character in the string after the nth iteration Examples: Input: m = 5, n = 2, k = 5 Output: 0 Explanation: Binary representation of m is "101", after one iteration binary representation will be "100110", and after second iteration binary representation will be "100101101001". Input: m = 5, n = 2, k = 1 Output: 1 Explanation: Binary representation of m is "101", after one iteration binary representation will be "100110", and after second iteration binary representation will be "100101101001".

      Answer 2

      Step-By-Step Implementation: Change the given number into a binary and store it in string s. Run loop n times. Run another loop for string length s to convert 0 to “01” and 1 to “10” and store in a temporary string s1. After completion of each iteration, assign string s1 to s. Finally, return the value of the kth index in string s. // C# Program to find kth character in a binary string. using System; class Program { // Function to store binary Representation static string BinaryRep(int m) { string s = ""; while (m > 0) { int tmp = m % 2; s += tmp.ToString(); m /= 2; } char[] charArray = s.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } // Function to find kth character static int FindKthChar(int n, int m, int k) { string s = BinaryRep(m); for (int x = 0; x < n; x++) { string s1 = ""; for (int y = 0; y < s.Length; y++) { if (s[y] == '1') s1 += "10"; else s1 += "01"; } // Assign s1 string in s string s = s1; } return s[k] - '0'; } // Driver Function static void Main() { int m = 5, n = 2, k = 8; Console.WriteLine(FindKthChar(n, m, k)); } }

    3. Question 3

      Given two strings s and p, the task is to find the smallest substring in s that contains all characters of p, including duplicates. If no such substring exists, return "". If multiple substrings of the same length are found, return the one with the smallest starting index.

      Answer 3

      The first step will be to find which block the k-th character will be after n iterations are performed. In the n'th iteration distance between any two consecutive characters initially will always be equal to 2^n. Consider input number m = 5 0th iteration: 101, distance = 0 1st iteration: 10 01 10, distance = 2 2nd iteration: 1001 0110 1001, distance = 4 3rd iteration: 10010110 01101001 10010110, distance = 8 We consider every bit of the input number as the start of a block and aim to determine the block number where the k-th bit will be located after n iterations. T Finding the Block & Offset Within Block With each iteration, the distance between bits doubles. After n iterations, the bits are spaced by 2ⁿ. So, to find which block contains the k-th bit, we compute: This offset can be thought of as a path in a binary tree formed by the repeated expansions. Each bit in the binary representation of this offset represents a decision point, a 1 indicates a flip in the bit. So, we count the number of 1s in the offset; this count gives us the number of bit flips applied to the base bit to reach the k-th position. Finally, we use the parity of this flip count: If the base bit b is 0, we start with '0'. An even number of flips keeps it '0', while an odd number of flips changes it to '1'. If the base bit b is 1, we start with '1'. An even number of flips keeps it '1', while an odd number of flips changes it to '0'. This flipping behavior is directly derived from the rules of expansion (0 → 01, 1 → 10), where each level toggles the bit depending on the path taken. Using this approach, we determine the final k-th bit efficiently in logarithmic time without constructing the full expanded string. // C# program to find k-th character after // n expansions using System; class GfG { // Function to count number of set bits (Hamming weight) static int countBits(int x) { int cnt = 0; while (x > 0) { cnt += x & 1; x >>= 1; } return cnt; } // Function to get total bits (like bit_length) static int totalBits(int m) { int bits = 0; while (m > 0) { m >>= 1; bits++; } return bits; } // Function to get k-th character after n expansions static char KthCharacter(int m, int n, int k) { int len = 1 << n; int bit_index = (k - 1) / len; // Get total bits in m (MSB to LSB) int total_bits = totalBits(m) - 1; int shift = total_bits - bit_index; int b = (m >> shift) & 1; int offset = (k - 1) % len; // Count set bits (parity) in offset int ones = countBits(offset); // Flip if parity is odd if (b == 0) { return (ones % 2 == 0) ? '0' : '1'; } else { return (ones % 2 == 0) ? '1' : '0'; } } // Driver code public static void Main() { int m = 5, k = 5, n = 3; Console.WriteLine(KthCharacter(m, n, k)); } }

Experiences are submitted by candidates and reflect their own accounts. Lampzi does not verify them. Browse more experiences