Table of contents
1.
Introduction
2.
HackWithInfy Coding Round-2 Information
3.
HackWithInfy Previous Year Coding Problems
3.1.
Beautiful Function 
3.2.
Paper Sheets 
3.3.
Special Prime
3.4.
The Dishes Problem
4.
Important Topics For HackwithInfy Round 2
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

Round - 2 Most Asked Questions HWI

Author Divyansh Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

HackWithInfy is a very famous country-wide coding competition for engineering students. HackWithInfy consists of two rounds, the first of which is an individual online exam, and the second of which is a Grand Finale. This year, HackWithInfy is available not only to final-year engineering students but also to second-and third-year engineering students in India who want to prove their worth and earn INR 3.5 lakhs in cash awards.

Source: Infosys

The exam structure and difficulty level of the HackWithInfy Coding Round is elaborated briefly in this blog. You'll discover a few sample coding problems based on the previous year's HackWithInfy Coding Section pattern. Thus, practicing these questions will help you grasp the section's difficulty level and test structure.

HackWithInfy Coding Round-2 Information

In the HackWithInfy test, there are two coding rounds:-

Round 1: In the first round of HackWithInfy, three code questions must be answered in three hours.

Round 2: This is the Grand Finale round, which will take place at Infosys Meridian. In round 2, the top 100 candidates from round 1 will be shortlisted.

HackWithInfy Previous Year Coding Problems

Beautiful Function 

Problem Statement: Define a Beautiful Function F(x) as follows: Add 1 to the value of x, and if the result contains any trailing zeros, remove them all.

Examples:

F(11) = 12

F(99) = 1(100 –> 10 –> 1)

F(29) = 3(30 –> 3)

Let us define a number that can be reached from x if we can apply the Beautiful Function to x a certain number of times (possibly zero) and get that number as a result.

Example: 3 can be deduced from the number 29953, as F(29953) -> F(2996) -> 3. 

You are given a number N. Your task is to determine how many numbers can be deduced from N.

Input Format: 

The first line contains an integer N, which represents the given number.

Constrains: 1 <= N <= 10^9

Input  Output Explanation
1 9 1,2,3,4,5,6,7,8,9 are reachable from 1
29953 20 29953, 29954, 29955, 29956, 29957, 29958, 29959, 2996, 2997, 2998, 2999, 3, 4, 5, 6, 7, 8, 9, 1, 2 are reachable from 29953
101 27 101, 102, 103, 104, 105, 106, 107, 108, 109, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 3, 4, 5, 6, 7, 8, 9, 1 are reachable from 101

Code: 

import java.util.*;
public class BeautifulFunction{
  
    public static int Beautiful_function( long n){
        int total = 0;
        int carry = 0;
        int current_dist = 0;
        
        //to convert long into string
        String str_num = Long.toString(n);
        int len = str_num.length() - 1;
                
        while(len > 0){
            current_dist = 10 - ((int)(str_num.charAt(len) - '0') + carry);
            total += current_dist;
            carry = 1;
            len -= 1;
        }
        total += 9;
        return total;
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number: ");
        long n = sc.nextLong();
        System.out.println("Number of values that can be deduced from the " +n+ " is " + Beautiful_function(n));
    }
}
You can also try this code with Online Java Compiler
Run Code

Approach: 

Adding 1 is not the best solution here. If we just kept adding 1 then the solution would lead to TLE, since the biggest N you would get is 10^9. As we can see in the input cases every solution is coming down to the unit digit. The moment we get the unit-digit number we need to add 9 in total anyways as you can see in the above code snippet. 

The idea is to traverse the number in reverse order. 

Let’s take a look at one input case:

Example: 29953

29953 = 299954-> 299955 -> 299954 -> 299955 -> 299956 -> 299957 -> 299958 -> 29959 (7)

Here, in order to get the result 7, we need to subtract the last unit-digit from 10. 

Let’s say there is a current_dist variable for counting the current total and a total variable which keeps on adding the current_dist in it each time when the traversal is shifted towards the next digit. 

Thus, 

current_dist = 10 - 3 = 7

Total = 7 

In addition to it, we need to add 1 each time the traversal is shifted. 

2996 = 2997 -> 2998 -> 2999 (4)

Here, 

current_dist = 10 - 6 = 4 

total = 7 + 4 = 11

3 = 4-> 5-> 6-> 7-> 8-> 9-> 1-> 2 (9)

Now, at this time we’ve encountered the unit digit number, so we would be adding 9 to the total result. 

Thus, 

Total = 7 + 4 + 9 = 20

Paper Sheets 

Sam is handed a rectangular piece of paper with the dimensions h*w, where h denotes height and w denotes width. Sam wants to fold the paper in the fewest possible moves such that its dimensions are h1*w1. The paper could only be folded parallel towards its edges, and the dimensions should be integers after folding. If h=8 and w=4, for example, fold the paper till it's h1, w1 = 6,1. First, fold the long edge 6 units to the side, resulting in a 6*4 paper. And then, Fold the page in three folds along the width 2 units edge to achieve a 6*1 page.

Input Format

First line contains an integer denotes h 

Seconds line contains an integer denotes w 

Third line contains an integer denotes h1

Fourth line contains an integer denotes w1

Constraints

1 <= h, w, h1, w1 <= 10^15

h1 <= h

w1 <= w

Output Format

Print the minimum moves required.

Sample Input:

2

3

2

2

Sample Output:

1

Code:

import java.util.*;
import static java.lang.Math.*;

public class PaperSheets{
    static int nimMoves(int h, int w , int h1 , int w1){
        int moves = 0 ; 
        while (w > w1){
            moves +=1; 
            if( w%2 == 0){
                w = (int)(w/2);
            }else{
                w = (int)(w+1)/2;
            }
        }

        while( h>h1 ){
            moves += 1;
            if( h%2 == 0){
                h = (int)(h/2);
            }else{
                h= (int)((h+1)/2);
            }
        }
        return moves; 
    }
    static int minMoves1(int h , int w , int h1, int w1){
        int final_ans = 0 ; 
        
        double  h_new = (Math.log(h)/Math.log(2));
        double  h1_new = (Math.log(h1)/Math.log(2));
        double  w_new = (Math.log(w)/Math.log(2));
        double  w1_new =  (Math.log(w1)/Math.log(2));

        if((int) h_new == h_new){
            final_ans += (int) h_new;
        }else{
            final_ans +=(int) h_new +1;
        }

        if((int) h1_new == h1_new){
            final_ans -= (int) h1_new;
        }else{
            final_ans -= (int) h1_new +1;
        }

        if((int) w_new == w_new){
            final_ans += w_new;
        }else{
            final_ans += (int)w_new +1;
        }

        if((int) w1_new == w1_new){
            final_ans -= (int)w1_new; 
        }else{
            final_ans -= (int) w1_new +1;
        }

        return final_ans; 
    }

        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            int h , w, h1,w1;
            System.out.print("Enter h : ");
            h = sc.nextInt();
            System.out.print("Enter w : ");
            w = sc.nextInt();
            System.out.print("Enter h1: ");
            h1 = sc.nextInt();
            System.out.print("Enter w1: ");
            w1 = sc.nextInt();
            System.out.println("The Minimum moves required are: "+nimMoves(h,w,h1,w1));
        }
}
You can also try this code with Online Java Compiler
Run Code

Approach

Here, we have two approaches to be considered.

Approach -1:

Step 1: Set total_moves to zero.

Step 2: if w is greater than w1, divide w by 2 and add 1 to the total_moves value.

Step 3: Repeat step 2 until the condition is broken.

Step 4: If h is bigger than h1, divide it by 2 and add 1 to the total_moves value.

Step 5: Repeat step 2 until the condition is broken.

Step 6: Print the value of total_moves.

Approach -2:

Step 1: Using base 2, get the log values for h, h1, w, w1 and store them in h_new, h1_new, w_new, w1_new.

Step 2: Then at last, Print the value of  h_new - h1_new + w_new - w1_new

Special Prime

Given an integer N where

N <= 10^50

And you need to Print the total no. of pairs (x.y) such that 

0 <= x <= n

0 <= y <= n

And F(x) + F(y) = Prime Number

Where F(x) = sum of the digits of x

Note: The pairs (x,y) and (y,x) are to be considered as one.

Input format

A single integer appears on the first line.

Output format

The output should also be a single integer.

Sample Input:

3

Sample Output:

5

Code: 

import java.util.Scanner; 
public class SpecialPrime{
    static int sod(int num){
        int sum = 0 ; 
        if(num < 10)
        return num ;
        while(num>0){
            sum += num%10 ; 
            num /= 10;
        }
        return sum ;
    }

    static boolean prime(int num){
        int i ; 
        if(num == 0 || num == 1){
            return false; 
        } 
        for(i= 2 ; i<num ;i++){
            if(num%i==0)
            return false; 
        }
        return true; 
    }

    static boolean find(int array1[][],int n , int x , int y ){
        int i ; 
        for(i = 0 ; i<n; i++){
            if (array1[i][0] == x && array1[i][1] == y)
            return false; 
        }
        return true; 
    }

    static int special_prime(int num){
        int i,j,x,y,r=0;
        int[][] arr = new int[100][100];
        for(i=0;i<num+1;i++){
            for(j=0;j<num+1;j++){
                x=sod(i);
                y=sod(j);
                if(prime(x+y)){
                    if(find(arr,r,x,y)&&find(arr,r,y,x)){
                        arr[r][0]=x;
                        arr[r][1]=y;
                        r++;
                    }
                }
            }
        }
        return r ; 
    }
    public static void main(String[]args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the integer N: ");
    int num = sc.nextInt();
    int res ;
    res = special_prime(num);
    System.out.println("Total No. of Pairs: "+res);     
    }
}
You can also try this code with Online Java Compiler
Run Code

Approach

So, we are given a number n, now we will maintain a set of pairs as set S. And then, Iterate i from 0 to n, similarly Iterate j from 0 to n. Determine whether the sum of digits of i and j is a prime number.

If the pair i,j is not found in set S, add it to S. Then, finally print the number of sets in S.

The Dishes Problem

Shyam has N various varieties of dishes lined up in a row: A1, A2,..., AN, where Ai signifies the type of the ith dish. He wants to select as many dishes as possible from the menu, but only if the following two requirements are satisfied:

  • He can select only one sort of dish.
  • Two adjacent dishes cannot be selected simultaneously.

Now the query is that Shyam wants to know which type of dish he should select so that he has the option of choosing the maximum number of dishes possible.

Example:

Given N = 9 and A = [1,2,2,1,2,1,1,1,1]

As we can see here, For type 1, Shyam has a maximum of four dishes to choose from. Selecting A1, A4, A7, and A9 is one of the possible ways of selecting four dishes of type 1.

For type 2, Shyam has a maximum of two dishes to choose from. One way is to select A3 and A5. In this situation, Shyam should go for type 1, which allows him to select comparatively more dishes.

Input Format

T, the number of test cases, appears on the first line. Then there are the test cases following T.

The first line of each test case contains a single integer N. And the N integers A1, A2,..., AN appears on the second line.

Constraints

1 <= T <= 10^3

1 <= N <= 10^3

1 <= Ai <= 10^3

Output Format

Print a single line for each test case containing one integer indicating the type of dish Shyam should select. Print the smallest response if there are multiple answers.

Sample Input:

3

5

1 2 2 1 2

6

1 1 1 1 1 1

8

1 2 2 2 3 4 2 1

Sample Output:

1

1

2

Code:

import java.util.Scanner;
public class TheDishesProblem{

    static int choose_dish(int array[], int n){
        int frequency[] = new int[1001];
        for(int i=0;i<1001;i++)
            frequency[i] = 0;
        int max = 0, result = 0;
        for( int i=0; i < n-1; i++ ){
            frequency[array[i]]++;
            if(array[i] == array[i+1])
                i++;
        }
        
        for( int i =1; i <= 1000; i++)
        {
            if ( frequency[i] > max )
            {
                max = frequency[i];
                result = i;
            }
        }
        return result;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of Test Cases: ");
        int test = sc.nextInt();
        while(test != 0)
        {
            System.out.print("Enter the number of Integers: ");
            int n = sc.nextInt();
            int array[] = new int[n];
            System.out.print("Enter the integers: ");
            for( int i =0; i < n; i++)
            {
                array[i] = sc.nextInt();
            }
            int result = choose_dish(array, n);
            System.out.println("Shyam should select: "+result);
            test--;
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Approach

Step 1: Create an empty array with a size of 1001. (since the maximum value is 1000 due to restrictions)

Step 2: Determine the frequency of each element. If the adjacent elements are the same, the frequency should be 1.

Step 3: Set max value to zero.

Step 4: Use condition 1001 and initialize for loop with it.

Step 5: If i's frequency of i is larger than the max value, replace max value with i's frequency and the result with i.

Step 6: Print the outcome. 

Important Topics For HackwithInfy Round 2

You may expect coding problems from the following list in HackwithInfy Round 2. As these topics have the most asked coding questions:

  1. Data StructuresArrayStackQueueLinked ListGraph)
  2. Dynamic Programming
  3. Mapping Concepts
  4. Backtracking
  5. Greedy Algorithms
  6. Mathematical Coding Problems

Frequently Asked Questions

1. How many participants are shortlisted for Round 2?

Ans - In July 2022, the top 100 competitors from Round 1 will participate in a four-day grand finale. Finalists will also be given the option to participate in a pre-placement interview and internships at Infosys for specialist technical jobs. 

2. How much time is given to solve round-2?

Ans - The candidate gets three complete hours to show their coding abilities in round 2 to submit their best answers before the deadline.

3. How to register for Round-2?

Ans - You don’t have to register for Round 2. The participation in this round will be on a team basis, as determined by the Company at its absolute discretion.

4. How many questions are asked in round-2? 

Ans - There are three questions given in round-2 of HackwithInfinity.

5. On which date Round 2 will be conducted?

Ans - This round will be conducted from July 1 to July 4, 2022. The Grand Finale is a 48-hour hackathon among teams.

Key Takeaways

In the above blog, we have learned the types of questions that we can face in HackWithInfy in detail and we have also discussed various frequent doubts about the exam and the registration process.

Check out Infosys Interview Experience to learn about their hiring process.

Now you have got a much better understanding of HackWithInfy. Hope you learned something. Coding Ninjas wishes you very good luck with the exam. Go ahead, Ninja!

Live masterclass