Zoho Coding Questions (PYQ's)

by Jasleen Chhabra | Updated on 23 July 2024

1. Write a program to give the following output for the given input

Eg 1: Input: a1b10

       Output: abbbbbbbbbb

Eg: 2: Input: b3c6d15

          Output: bbbccccccddddddddddddddd

The number varies from 1 to 99.

Solutions: 

C

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

 

void expandString(const char *input) {

    while (*input) {

        char ch = *input++;

        int num = 0;

        while (isdigit(*input)) {

            num = num * 10 + (*input++ - '0');

        }

        for (int i = 0; i < num; i++) {

            putchar(ch);

        }

    }

    putchar('\n');

}

 

int main() {

    char input[100];

    printf("Enter input: ");

    scanf("%s", input);

    expandString(input);

    return 0;

}

 

C++

 

#include <iostream>

#include <string>

#include <cctype>

 

void expandString(const std::string &input) {

    for (size_t i = 0; i < input.length();) {

        char ch = input[i++];

        int num = 0;

        while (i < input.length() && std::isdigit(input[i])) {

            num = num * 10 + (input[i++] - '0');

        }

        for (int j = 0; j < num; j++) {

            std::cout << ch;

        }

    }

    std::cout << std::endl;

}

 

int main() {

    std::string input;

    std::cout << "Enter input: ";

    std::cin >> input;

    expandString(input);

    return 0;

}

 

Java

 

import java.util.Scanner;

 

public class ExpandString {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter input: ");

        String input = scanner.next();

        scanner.close();

        expandString(input);

    }

 

    public static void expandString(String input) {

        for (int i = 0; i < input.length();) {

            char ch = input.charAt(i++);

            int num = 0;

            while (i < input.length() && Character.isDigit(input.charAt(i))) {

                num = num * 10 + (input.charAt(i++) - '0');

            }

            for (int j = 0; j < num; j++) {

                System.out.print(ch);

            }

        }

        System.out.println();

    }

}

 

Python

 

def expand_string(input):

    i = 0

    while i < len(input):

        ch = input[i]

        i += 1

        num = 0

        while i < len(input) and input[i].isdigit():

            num = num * 10 + int(input[i])

            i += 1

        print(ch * num, end='')

    print()

 

input_str = input("Enter input: ")

expand_string(input_str)


2. Write a program to sort the elements in odd positions in descending order and elements in ascending order

Eg 1: Input: 13,2 4,15,12,10,5

        Output: 13,2,12,10,5,15,4

Eg 2: Input: 1,2,3,4,5,6,7,8,9

        Output: 9,2,7,4,5,6,3,8,1
 

C

#include <stdio.h>

#include <stdlib.h>

 

int compare_desc(const void *a, const void *b) {

    return (*(int *)b - *(int *)a);

}

 

int compare_asc(const void *a, const void *b) {

    return (*(int *)a - *(int *)b);

}

 

void sort_odd_even_positions(int *arr, int size) {

    int odd_count = (size + 1) / 2;

    int even_count = size / 2;

    int *odd_positions = (int *)malloc(odd_count * sizeof(int));

    int *even_positions = (int *)malloc(even_count * sizeof(int));

 

    int odd_index = 0, even_index = 0;

    for (int i = 0; i < size; i++) {

        if (i % 2 == 0) {

            odd_positions[odd_index++] = arr[i];

        } else {

            even_positions[even_index++] = arr[i];

        }

    }

 

    qsort(odd_positions, odd_count, sizeof(int), compare_desc);

    qsort(even_positions, even_count, sizeof(int), compare_asc);

 

    odd_index = 0;

    even_index = 0;

    for (int i = 0; i < size; i++) {

        if (i % 2 == 0) {

            arr[i] = odd_positions[odd_index++];

        } else {

            arr[i] = even_positions[even_index++];

        }

    }

 

    free(odd_positions);

    free(even_positions);

}

 

int main() {

    int arr[] = {13, 2, 4, 15, 12, 10, 5};

    int size = sizeof(arr) / sizeof(arr[0]);

 

    sort_odd_even_positions(arr, size);

 

    for (int i = 0; i < size; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");

 

    return 0;

}

C++

#include <iostream>

#include <vector>

#include <algorithm>

 

void sort_odd_even_positions(std::vector<int>& arr) {

    std::vector<int> odd_positions, even_positions;

 

    for (size_t i = 0; i < arr.size(); ++i) {

        if (i % 2 == 0) {

            odd_positions.push_back(arr[i]);

        } else {

            even_positions.push_back(arr[i]);

        }

    }

 

    std::sort(odd_positions.begin(), odd_positions.end(), std::greater<int>());

    std::sort(even_positions.begin(), even_positions.end());

 

    size_t odd_index = 0, even_index = 0;

    for (size_t i = 0; i < arr.size(); ++i) {

        if (i % 2 == 0) {

            arr[i] = odd_positions[odd_index++];

        } else {

            arr[i] = even_positions[even_index++];

        }

    }

}

 

int main() {

    std::vector<int> arr = {13, 2, 4, 15, 12, 10, 5};

 

    sort_odd_even_positions(arr);

 

    for (int num : arr) {

        std::cout << num << " ";

    }

    std::cout << std::endl;

 

    return 0;

}

Java

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

 

public class SortOddEven {

    public static void main(String[] args) {

        int[] arr = {13, 2, 4, 15, 12, 10, 5};

        sortOddEvenPositions(arr);

 

        for (int num : arr) {

            System.out.print(num + " ");

        }

        System.out.println();

    }

 

    public static void sortOddEvenPositions(int[] arr) {

        List<Integer> oddPositions = new ArrayList<>();

        List<Integer> evenPositions = new ArrayList<>();

 

        for (int i = 0; i < arr.length; i++) {

            if (i % 2 == 0) {

                oddPositions.add(arr[i]);

            } else {

                evenPositions.add(arr[i]);

            }

        }

 

        Collections.sort(oddPositions, Collections.reverseOrder());

        Collections.sort(evenPositions);

 

        int oddIndex = 0, evenIndex = 0;

        for (int i = 0; i < arr.length; i++) {

            if (i % 2 == 0) {

                arr[i] = oddPositions.get(oddIndex++);

            } else {

                arr[i] = evenPositions.get(evenIndex++);

            }

        }

    }

}

Python

def sort_odd_even_positions(arr):

    odd_positions = [arr[i] for i in range(len(arr)) if i % 2 == 0]

    even_positions = [arr[i] for i in range(len(arr)) if i % 2 != 0]

 

    odd_positions.sort(reverse=True)

    even_positions.sort()

 

    odd_index = 0

    even_index = 0

    for i in range(len(arr)):

        if i % 2 == 0:

            arr[i] = odd_positions[odd_index]

            odd_index += 1

        else:

            arr[i] = even_positions[even_index]

            even_index += 1

 

arr = [13, 2, 4, 15, 12, 10, 5]

sort_odd_even_positions(arr)

print(arr)

 

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

sort_odd_even_positions(arr)

print(arr)


3. Write a program to print the following output for the given input. You can assume the string is of odd length

Eg 1: Input: 12345

       Output:

1       5

  2   4

    3

  2  4

1      5

Eg 2: Input: geeksforgeeks

         Output:

g                         s

  e                     k

    e                 e

      k             e

        s         g

          f      r

             o

          f     r

        s         g

      k             e

    e                 e

  e                      k

g                          s

 

C

#include <stdio.h>

#include <string.h>

 

void printPattern(char *str) {

    int len = strlen(str);

    int mid = len / 2;

 

    for (int i = 0; i < len; i++) {

        for (int j = 0; j < len; j++) {

            if (j == i) {

                printf("%c", str[j]);

            } else if (j == len - i - 1) {

                printf("%c", str[j]);

            } else {

                printf(" ");

            }

        }

        printf("\n");

    }

}

 

int main() {

    char input1[] = "12345";

    char input2[] = "geeksforgeeks";

 

    printf("Output for input: %s\n", input1);

    printPattern(input1);

 

    printf("\nOutput for input: %s\n", input2);

    printPattern(input2);

 

    return 0;

}

C++

#include <iostream>

#include <string>

 

void printPattern(const std::string &str) {

    int len = str.length();

 

    for (int i = 0; i < len; ++i) {

        for (int j = 0; j < len; ++j) {

            if (j == i || j == len - i - 1) {

                std::cout << str[j];

            } else {

                std::cout << " ";

            }

        }

        std::cout << std::endl;

    }

}

 

int main() {

    std::string input1 = "12345";

    std::string input2 = "geeksforgeeks";

 

    std::cout << "Output for input: " << input1 << std::endl;

    printPattern(input1);

 

    std::cout << "\nOutput for input: " << input2 << std::endl;

    printPattern(input2);

 

    return 0;

}

Java

public class PrintPattern {

    public static void main(String[] args) {

        String input1 = "12345";

        String input2 = "geeksforgeeks";

 

        System.out.println("Output for input: " + input1);

        printPattern(input1);

 

        System.out.println("\nOutput for input: " + input2);

        printPattern(input2);

    }

 

    public static void printPattern(String str) {

        int len = str.length();

 

        for (int i = 0; i < len; i++) {

            for (int j = 0; j < len; j++) {

                if (j == i || j == len - i - 1) {

                    System.out.print(str.charAt(j));

                } else {

                    System.out.print(" ");

                }

            }

            System.out.println();

        }

    }

}

Python

def print_pattern(s):

    length = len(s)

    for i in range(length):

        for j in range(length):

            if j == i or j == length - i - 1:

                print(s[j], end='')

            else:

                print(' ', end='')

        print()

 

input1 = "12345"

input2 = "geeksforgeeks"

 

print("Output for input:", input1)

print_pattern(input1)

 

print("\nOutput for input:", input2)

print_pattern(input2)


4. Find if a String2 is substring of String1. If it is, return the index of the first occurrence. else return -1.

Eg 1:Input:

        String 1: test123string

         String 2: 123

        Output: 4

Eg 2: Input:

        String 1: testing12

        String 2: 1234

        Output: -1

C

#include <stdio.h>

#include <string.h>

 

int findSubstring(char *str1, char *str2) {

    char *pos = strstr(str1, str2);

    if (pos) {

        return pos - str1;

    }

    return -1;

}

 

int main() {

    char str1[] = "test123string";

    char str2[] = "123";

    printf("Output: %d\n", findSubstring(str1, str2));  // Output: 4

 

    char str3[] = "testing12";

    char str4[] = "1234";

    printf("Output: %d\n", findSubstring(str3, str4));  // Output: -1

 

    return 0;

}

C++

#include <iostream>

#include <string>

 

int findSubstring(const std::string &str1, const std::string &str2) {

    size_t pos = str1.find(str2);

    if (pos != std::string::npos) {

        return pos;

    }

    return -1;

}

 

int main() {

    std::string str1 = "test123string";

    std::string str2 = "123";

    std::cout << "Output: " << findSubstring(str1, str2) << std::endl;  // Output: 4

 

    std::string str3 = "testing12";

    std::string str4 = "1234";

    std::cout << "Output: " << findSubstring(str3, str4) << std::endl;  // Output: -1

 

    return 0;

}

Java

public class FindSubstring {

    public static void main(String[] args) {

        String str1 = "test123string";

        String str2 = "123";

        System.out.println("Output: " + findSubstring(str1, str2));  // Output: 4

 

        String str3 = "testing12";

        String str4 = "1234";

        System.out.println("Output: " + findSubstring(str3, str4));  // Output: -1

    }

 

    public static int findSubstring(String str1, String str2) {

        int index = str1.indexOf(str2);

        return index;

    }

}

Python

def find_substring(str1, str2):

    index = str1.find(str2)

    return index

 

str1 = "test123string"

str2 = "123"

print("Output:", find_substring(str1, str2))  # Output: 4

 

str3 = "testing12"

str4 = "1234"

print("Output:", find_substring(str3, str4))  # Output: -1

 


5. Given two sorted arrays, merge them such that the elements are not repeated

Eg 1: Input:

        Array 1: 2,4,5,6,7,9,10,13

        Array 2: 2,3,4,5,6,7,8,9,11,15

       Output:

       Merged array: 2,3,4,5,6,7,8,9,10,11,13,15

 

C

#include <stdio.h>

 

void mergeArrays(int arr1[], int len1, int arr2[], int len2, int merged[], int *merged_len) {

    int i = 0, j = 0, k = 0;

   

    while (i < len1 && j < len2) {

        if (arr1[i] < arr2[j]) {

            if (k == 0 || merged[k - 1] != arr1[i]) {

                merged[k++] = arr1[i];

            }

            i++;

        } else if (arr1[i] > arr2[j]) {

            if (k == 0 || merged[k - 1] != arr2[j]) {

                merged[k++] = arr2[j];

            }

            j++;

        } else {

            if (k == 0 || merged[k - 1] != arr1[i]) {

                merged[k++] = arr1[i];

            }

            i++;

            j++;

        }

    }

   

    while (i < len1) {

        if (k == 0 || merged[k - 1] != arr1[i]) {

            merged[k++] = arr1[i];

        }

        i++;

    }

   

    while (j < len2) {

        if (k == 0 || merged[k - 1] != arr2[j]) {

            merged[k++] = arr2[j];

        }

        j++;

    }

   

    *merged_len = k;

}

 

int main() {

    int arr1[] = {2, 4, 5, 6, 7, 9, 10, 13};

    int arr2[] = {2, 3, 4, 5, 6, 7, 8, 9, 11, 15};

    int len1 = sizeof(arr1) / sizeof(arr1[0]);

    int len2 = sizeof(arr2) / sizeof(arr2[0]);

    int merged[len1 + len2];

    int merged_len;

 

    mergeArrays(arr1, len1, arr2, len2, merged, &merged_len);

 

    printf("Merged array: ");

    for (int i = 0; i < merged_len; i++) {

        printf("%d ", merged[i]);

    }

    printf("\n");

 

    return 0;

}

C++

#include <iostream>

#include <vector>

#include <algorithm>

 

std::vector<int> mergeArrays(const std::vector<int>& arr1, const std::vector<int>& arr2) {

    std::vector<int> merged;

    int i = 0, j = 0;

   

    while (i < arr1.size() && j < arr2.size()) {

        if (arr1[i] < arr2[j]) {

            if (merged.empty() || merged.back() != arr1[i]) {

                merged.push_back(arr1[i]);

            }

            i++;

        } else if (arr1[i] > arr2[j]) {

            if (merged.empty() || merged.back() != arr2[j]) {

                merged.push_back(arr2[j]);

            }

            j++;

        } else {

            if (merged.empty() || merged.back() != arr1[i]) {

                merged.push_back(arr1[i]);

            }

            i++;

            j++;

        }

    }

   

    while (i < arr1.size()) {

        if (merged.empty() || merged.back() != arr1[i]) {

            merged.push_back(arr1[i]);

        }

        i++;

    }

   

    while (j < arr2.size()) {

        if (merged.empty() || merged.back() != arr2[j]) {

            merged.push_back(arr2[j]);

        }

        j++;

    }

   

    return merged;

}

 

int main() {

    std::vector<int> arr1 = {2, 4, 5, 6, 7, 9, 10, 13};

    std::vector<int> arr2 = {2, 3, 4, 5, 6, 7, 8, 9, 11, 15};

    std::vector<int> merged = mergeArrays(arr1, arr2);

 

    std::cout << "Merged array: ";

    for (int num : merged) {

        std::cout << num << " ";

    }

    std::cout << std::endl;

 

    return 0;

}

Java

 

import java.util.ArrayList;

import java.util.List;

 

public class MergeSortedArrays {

    public static void main(String[] args) {

        int[] arr1 = {2, 4, 5, 6, 7, 9, 10, 13};

        int[] arr2 = {2, 3, 4, 5, 6, 7, 8, 9, 11, 15};

        List<Integer> merged = mergeArrays(arr1, arr2);

 

        System.out.print("Merged array: ");

        for (int num : merged) {

            System.out.print(num + " ");

        }

        System.out.println();

    }

 

    public static List<Integer> mergeArrays(int[] arr1, int[] arr2) {

        List<Integer> merged = new ArrayList<>();

        int i = 0, j = 0;

 

        while (i < arr1.length && j < arr2.length) {

            if (arr1[i] < arr2[j]) {

                if (merged.isEmpty() || !merged.get(merged.size() - 1).equals(arr1[i])) {

                    merged.add(arr1[i]);

                }

                i++;

            } else if (arr1[i] > arr2[j]) {

                if (merged.isEmpty() || !merged.get(merged.size() - 1).equals(arr2[j])) {

                    merged.add(arr2[j]);

                }

                j++;

            } else {

                if (merged.isEmpty() || !merged.get(merged.size() - 1).equals(arr1[i])) {

                    merged.add(arr1[i]);

                }

                i++;

                j++;

            }

        }

 

        while (i < arr1.length) {

            if (merged.isEmpty() || !merged.get(merged.size() - 1).equals(arr1[i])) {

                merged.add(arr1[i]);

            }

            i++;

        }

 

        while (j < arr2.length) {

            if (merged.isEmpty() || !merged.get(merged.size() - 1).equals(arr2[j])) {

                merged.add(arr2[j]);

            }

            j++;

        }

 

        return merged;

    }

}

Python

def merge_arrays(arr1, arr2):

    merged = []

    i, j = 0, 0

   

    while i < len(arr1) and j < len(arr2):

        if arr1[i] < arr2[j]:

            if not merged or merged[-1] != arr1[i]:

                merged.append(arr1[i])

            i += 1

        elif arr1[i] > arr2[j]:

            if not merged or merged[-1] != arr2[j]:

                merged.append(arr2[j])

            j += 1

        else:

            if not merged or merged[-1] != arr1[i]:

                merged.append(arr1[i])

            i += 1

            j += 1

   

    while i < len(arr1):

        if not merged or merged[-1] != arr1[i]:

            merged.append(arr1[i])

        i += 1

   

    while j < len(arr2):

        if not merged or merged[-1] != arr2[j]:

            merged.append(arr2[j])

        j += 1

   

    return merged

 

arr1 = [2, 4, 5, 6, 7, 9, 10, 13]

arr2 = [2, 3, 4, 5, 6, 7, 8, 9, 11, 15]

merged = merge_arrays(arr1, arr2)

print("Merged array:", merged)

 


6. Using Recursion reverse the string such as

Eg 1: Input: one two three

      Output: three two one

Eg 2: Input: I love india

      Output: india love I

 

C

#include <stdio.h>

#include <string.h>

 

void reverseWords(char *str, int start, int end) {

    if (start >= end) {

        return;

    }

 

    int i = end;

    while (i >= start && str[i] != ' ') {

        i--;

    }

 

    printf("%s ", &str[i + 1]);

    reverseWords(str, start, i - 1);

}

 

int main() {

    char input1[] = "one two three";

    char input2[] = "I love india";

 

    printf("Output: ");

    reverseWords(input1, 0, strlen(input1) - 1);

    printf("\n");

 

    printf("Output: ");

    reverseWords(input2, 0, strlen(input2) - 1);

    printf("\n");

 

    return 0;

}

C++

#include <iostream>

#include <string>

 

void reverseWords(const std::string &str, int start, int end) {

    if (start >= end) {

        return;

    }

 

    int i = end;

    while (i >= start && str[i] != ' ') {

        i--;

    }

 

    std::cout << str.substr(i + 1, end - i) << " ";

    reverseWords(str, start, i - 1);

}

 

int main() {

    std::string input1 = "one two three";

    std::string input2 = "I love india";

 

    std::cout << "Output: ";

    reverseWords(input1, 0, input1.length() - 1);

    std::cout << std::endl;

 

    std::cout << "Output: ";

    reverseWords(input2, 0, input2.length() - 1);

    std::cout << std::endl;

 

    return 0;

}

Java

public class ReverseWords {

    public static void main(String[] args) {

        String input1 = "one two three";

        String input2 = "I love india";

 

        System.out.print("Output: ");

        reverseWords(input1, 0, input1.length() - 1);

        System.out.println();

 

        System.out.print("Output: ");

        reverseWords(input2, 0, input2.length() - 1);

        System.out.println();

    }

 

    public static void reverseWords(String str, int start, int end) {

        if (start > end) {

            return;

        }

 

        int i = end;

        while (i >= start && str.charAt(i) != ' ') {

            i--;

        }

 

        System.out.print(str.substring(i + 1, end + 1) + " ");

        reverseWords(str, start, i - 1);

    }

}

Python

def reverse_words(s, start, end):

    if start > end:

        return

   

    i = end

    while i >= start and s[i] != ' ':

        i -= 1

 

    print(s[i+1:end+1], end=' ')

    reverse_words(s, start, i-1)

 

input1 = "one two three"

input2 = "I love india"

 

print("Output:", end=' ')

reverse_words(input1, 0, len(input1) - 1)

print()

 

print("Output:", end=' ')

reverse_words(input2, 0, len(input2) - 1)

print()



FAQ

Any Questions?
Look Here.