Coding Challenges

Problems on Arrays

  1. Given a list of N (1 <= N <= 50000) integer. You are required to compute the number of pairs of indexes whose values are the same, and also the maximum distance between two indexes with equal values. N will be given in the first line of the input. N lines follow each one with one integer from the list. If no identical pairs are found, print NA, otherwise print a single line with two integers, the number of identical pairs, and the maximum distance between indexes with equal values.

    Sample Input1:
    6
    1
    2
    3
    2
    4
    2
    Sample Output1:
    2 4
    Sample Input2:
    4
    1
    2
    3
    4
    Sample Output2:
    NA

    Click Here for Solution

  2. The e-commerce company ‘Easy Shopping’ displays some specific products for its premium customers on its user interface. The company shortlisted a list of N products. The list contains the price of each product. The company will select random products for display. The selection criteria states that only those products whose price is a perfect cube number will be selected for display. The selection process is automated and is done by the company’s system. The company wishes to know the total count of the products selected for display.
    Write an algorithm to find the count of products that will be displayed.

    Input Format:
    The first line of the input consists of an integer – numProducts, representing the number of products(N).
    The second line consists of N space-separated integers – price0, price1,…,priceN-1 representing the product price of the N shortlisted products.
    Output Format:
    Print an integer representing the number of products that will be selected for display.
    Output Format:
    0<=numProducts<=10^6
    0<=price<=10^6
    Sample Input:
    6
    125 216 54 81 48 343
    Sample Output:
    3

    Click Here for Solution

  3. You are given an array Arr of N integers. You need to find the length of longest Peak Subarray of the given array Arr.
    Peak Array: Array A of length K is called as a peak Array, if following condition holds true:
    A[1]<A[2]<A[3]<….<A[K].
    Formally any strictly increasing array is called a Peak Array.
    Subarray: Subarray of any array is a contiguous part of the array (with length greater than zero).
    Your task is to find the length of the longest peak subarray of a given array.
    Note: Array of length 1 is always a Peak Array.

    Input Format:
    First line of input, single integer value N
    Second line of input, contains N integers with space-separation for Array Arr.
    Output Format:
    Print an integer that represents the length of longest peak subarray.
    Sample Input:
    5
    3 3 9 8 4
    Sample Output:
    2

    Click Here for Solution

See More...

Problems on Strings

  1. The online tutoring platform ‘TutoringGuide’ whishes to organize son online assessment test in various subjects for its students. The subjects will be identified by its examination code is a string tagged ‘a-z’ or ‘A-Z’. The examination code is auto generated by the system. While generating the examination code, some bug in the system allowed the entry of numbers automatically. Now the professional needs to identify the total count of numbers present in the examination code before creating a script to remove the bug.
    Write an algorithm to find out the total count of the numbers present in the examination codes.

    Input Format:
    The input consists of a string – textInput, representing the examination code.
    Output Format:
    Print an integer representing the count of numbers present in the examination code.
    Note: textInput consist of whitespaces, alphabets and numbers only.
    Sample Input:
    aaA 234bcB sadC5678 hsagd
    Sample Output:
    7

    Click Here for Solution

  2. "FarwayHome" is an online platform that provides its users with property buying and selling services. Through this platform a buyer can register a plot online. Each buyer is provided with a plot registration number. When a customer buys a plot from a seller, the system automatically generates a plot allotment number through the registration number. To generate the plot allotment number, each even digit of the plot registration number is replaced by the Kth digit after it in the number. The odd digits in the number are not replaced. For the last K digits, the series of digits in the plot number is considered in the cyclic manner.
    Write an algorithm to find the plot allotment number.

    Input Format:
    The first line of the input consists of an integer – plotRegistrationNum, representing the plot registration number.
    The second line consists of an integer – valueK, an integer representing the value of K.
    Output Format:
    Print an integer representing the plot allotment number.
    Note: Consider zero(0) as an even and one(1) as an odd number.
    Constraints:
    0 < plotRegistrationNum >= 10^5
    Sample Input:
    25643
    3
    Sample Output:
    45253

    Click Here for Solution

  3. Create unique device names to be used in a residential IoT (Internet of Things) system. If a device name already exists in the system, an integer number is added at the end of the name to make it unique. The integer added starts with 1 and is incremented by 1 for each new request of an existing device name. Given a list of device name requests, process all requests and display an array of the corresponding unique device names.

    Constraints
    • 1<=n<=10^4
    • 1<=length of devicenames[i]<=20
    • devicenames[i] contains only lowercase English letters in the range ASCII[a-z].
    Input Format:
    The first line contains an integer, n, denoting the size of the array.
    Each line i of the n subsequent lines (where 0<=i<n) contains a string.
    Output Format:
    Display unique device names in n lines.
    Sample Input:
    6
    switch
    tv
    switch
    tv
    switch
    tv
    Sample Output:
    switch
    tv
    switch1
    tv1
    switch2
    tv2

    Click Here for Solution

See More...

Problems using Classes

  1. Understand the below program and print the details of the student as shown in the sample test cases.

    • Student class is defined with public variables name, age, group and report.
    • Define setDetails() method that sets the student name, age, group and report values.
    • Define getDetails() method that prints all the details of the student.
    • Create an instance of class Student.
    • Set the name, age, group and report values of the student by taking the input from the student and call the setDetails() values
    • Now print the details of Student by calling the method getDetails()
    Sample Input:
    Govardhan
    37
    IT
    Pass
    Sample Output:
    Govardhan 37 IT Pass

    Click Here for Solution

  2. Let's write a simple program:

    • A base class Person and a derived class Student with Person as its base class.
    • Add two methods in the base class: setname()-takes the parameter name which sets the name value, and getname() which prints the name.
    • Add two methods in the derived class: setage()-takes the parameter age which sets the age value, and getage() which prints the age.
    • Create an instance of Student and name it as s1.
    • Take name and age as inputs from the console.
    • Call the setname() and setage() on this instance by passing the name and age parameters.
    • Call the getname() and getage() on this class, which prints the passed parameters
    Sample Input:
    Govardhan
    37
    Sample Output:
    Govardhan
    37

    Click Here for Solution

See More...