0% found this document useful (0 votes)
60 views

Interview Bit Problem Solving

Uploaded by

Harini
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Interview Bit Problem Solving

Uploaded by

Harini
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

INTERVIEW BIT PROBLEM SOLVING:

1)CHIPS FACTORY:

-------

2)Greater than All:

Given an integer array A.


Find the count of elements whose value is greater than all of its previous
elements.

Example Input
Input 1:
A = [1, 2, 3, 4]
Input 2:

A = [1, 1, 2, 2]

Example Output
Output 1:
4
Output 2:

public class Solution {


public int solve(int[] A) {

int n = A.length;

int max = A[0];


int cnt =1;

for(int i =1;i<n;i++){
if(A[i] > max){
max = A[i];
cnt++;
}
}
return cnt;
}
}

---------------------
3) Pythagorean Triplets:

A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2.


Find the number of pythagorean triplets such that all the elements of the triplet
are less than or equal to A.

Example Input
Input 1:
A = 5
Input 2:
A = 13

Example Output
Output 1:
1
Output 2:

Example Explanation
Explanation 1:
Then only triplet is {3, 4, 5}
Explanation 2:

The triplets are {3, 4, 5}, {6, 8, 10}, {5, 12, 13}.

public class Solution {


public int solve(int A) {

int count = 0;

for(int a=1 ; a<=A;a++){


for(int b =a;b<=A;b++){
int sq = a*a +b*b;
int c = (int) Math.sqrt(sq);

if( c <= A && c*c == sq){


count++;
}
}
}
return count;
}
}

----------

4) Diagonal Flip:

Problem Description

Given a square binary matrix of drimensions N×N.


Flip the matrix diagonally and return the matrix.

Example Input
Input 1:
A = 4
B = [[1, 0],
[0, 1]]
Input 2:

A = [[1, 0],
[1, 0]]
Example Output
Output 1:
[[1, 0],
[0, 1]]
Output 2:

[[1, 1],
[0, 0]]

public class Solution {


public int[][] solve(int[][] A) {

int n =A.length;

int [][] B = new int[n][n];

for(int i =0;i<n;i++){
for(int j=0;j<n;j++){

B[i][j] = A[j][i];

}
}
return B;
}
}

--------

5) POsitive negative:

Problem Description

Given an integer array A.


Find the number of positive and negative integers in it an return them in an array.

Example Input
Input 1:
A = [1, 2, 3]
Input 2:

A = [1, 0, -1]

Example Output
Output 1:
[3, 0]
Output 2:

[1, 1]

Example Explanation
Explanation 1:
Positive values are [1, 2, 3].
There are no negative values.
Explanation 2:

Only positive value is [1].


Only negative value is [-1].

public class Solution {


public int[] solve(int[] A) {

int[] result = new int[2];

int pos = 0;
int neg = 0;

for(int num : A){


if(num > 0){
pos++;

}
else if(num < 0){
neg++;
}
}

result[0] = pos;
result[1] = neg;

return result;
}
}

------------
6)

----------

7)Occurence of each number :

You are given an integer array A.

You have to find the number of occurences of each number.

Return an array containing only the occurences with the smallest value's occurence
first.

For example, A = [4, 3, 3], you have to return an array [2, 1], where 2 is the
number of occurences for element 3,
and 1 is the number of occurences for element 4. But, 2 comes first because 3 is
smaller than 4.

public class Solution {


public ArrayList<Integer> findOccurences(ArrayList<Integer> A) {
int n = A.size();
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0; i<n; i++) {
int count = map.getOrDefault(A.get(i), 0);
map.put(A.get(i), count+1);
}
Map<Integer, Integer> treeMap = new TreeMap<>(map);
ArrayList<Integer> list = new ArrayList<>();
for(Map.Entry<Integer, Integer> entry: treeMap.entrySet())
list.add(entry.getValue());
return list;
}
}

---------------

8)Greater of lesser:

Input 1:
A = [1, 2, 3, 4]
B = [5, 6, 7, 8]
C = 4

Output 1:
0

Example Explanation
There are no integers greater than C in A.
There are no integers less than C in B.

public class Solution {


public int solve(int[] A, int[] B, int C) {
int a1 = A.length;
int b1 = B.length;

int countA = 0;
int countB = 0;

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


if(A[i] > C) {
countA++;
}
}

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


if(B[i] < C) {
countB++;
}
}

int max = Math.max(countA, countB);

return max;
}
}

---

Find last digit :

Input 1:
A = 11
B = 11

Output 1:
1

1111 = 285311670611, hence last digit is 1.

public class Solution {


public int solve(String A, String B) {
int a = A.charAt(A.length()-1) - '0';
int b = Integer.parseInt(B.substring(B.length()-2));
long ans = (long)Math.pow(a,(b%4+4));
return (int)(ans%10);
}

-----

Digital Root:

Input 1:
A = 99
Output 1:
9
Example Explanation
99 -> 9+9 = 18 -> 1+8 = 9

public class Solution {


public int solve(int A) {
int root =0;

while (A>0 || root >9){


if(A==0){
A = root;
root =0;
}
root += A%10;
A/=10;

}
return root;
}
}
--------

Noble Integer :

Input 1:

A = [3, 2, 1, 3]

Output 1:

Explanation 1:
For integer 2, there are 2 greater elements in the array. So, return 1.

public class Solution {


public int solve(ArrayList<Integer> A) {

Collections.sort(A);
int n = A.size();

for(int i = 0;i<n;i++){
if(A.get(i) >= 0 && A.get(i) == n - i - 1){
if(i == n-1 || A.get(i) < A.get(i+1)){
return 1;
}
}
}
return -1;
}
}

--------

Reverse integer:

You are given an integer N and the task is to reverse the digits of the given
integer. Return 0 if the result overflows and does not fit in a 32 bit signed
integer

Input 1:
x = 123
Input 2:
x = -123

Output 1:

321
Ouput 2:
-321

If the given integer is negative like -123 the output is also negative -321.

public class Solution {


public int reverse(int A) {
boolean isNegative = false;

if (A < 0) {
isNegative = true;
A = -A;
}

long rev = 0;

while (A > 0) {
int dig = A % 10;
rev = (rev * 10) + dig;
A /= 10;
}

if (isNegative) {
if (rev > Integer.MAX_VALUE)
return 0;
rev = -rev;
} else {
if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE)
return 0;
}

return (int) rev;


}
}

----------

You might also like