Interview Questions

Java

Print first K Max Numbers

public void printFirstKMaxNumbers(int[] nums,int k){
       
        int[] maxNums = new int[k];
        for(int n:nums){
           int f = n;
                for(int j=0;j<k;j++)
                {
                        int temp = 0;
                        if(maxNums[j]<f){
                               
                                temp=maxNums[j];
                               
                                maxNums[j]=f;
                                f=temp;
                               
                        }
                }
               
        }
       
        for(int j=0;j<k;j++)
        {
                System.out.println(maxNums[j]);
        }
    }

Queue - Using 2 Stacks

package com.test;

import java.util.Scanner;
import java.util.Stack;

public class StkQueue {

        private Stack<Integer> head = new Stack<>();
        private Stack<Integer> tail = new Stack<>();
       
        public static void main(String[] args){
               
                Scanner sc = new Scanner(System.in);
                StkQueue queue = new StkQueue();
                int N = sc.nextInt();
               
                for(int i=0;i<N;i++)
                {
                       
                        int c = sc.nextInt();
                       
                        switch(c){
                       
                        case 1:
                                queue.enqueue(sc.nextInt());
                                break;
                        case 2:
                                queue.dequeue();
                                break;
                        case 3 :

Coding Challenge - Java

Given an array of N distinct elements. Let M1 and M2 be the smallest and the next smallest element in the interval [L,R] where 1 <= L < R < N.

S(i) = (((M1 AND M2) XOR (M1 OR M2)) AND (M1 XOR M2))

Your task is to find the maximum possible value of S.

Input Format

First line contains integer N.
Second line contains Nintegers, representing elements of the array A[] .

Constraints

Output Format

Print the value of maximum possible value of S(i).

Sample Input

5
9 6 3 5 2

Sample Output

15

 

Poisonous Plants - Stack Span Problem - Coding Challenge

There are plants in a garden. Each of these plants has been added with some amount of pesticide. After each day, if any plant has more pesticide than the plant at its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each plant. Print the number of days after which no plant dies, i.e. the time after which there are no plants with more pesticide content than the plant to their left.

Input Format

Pancake Sorting

Given an an unsorted array, sort the given array. You are allowed to do only following operation on array.

flip(arr, i): Reverse array from 0 to i

Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible, the goal is to sort the sequence in as few reversals as possible.

package com.test;

public class PancakeTest{
       
        public static void main(String[] args){
               
                int[]  arr = {102, 77,5,4,1,2,3,8,9};
               
                // 1 2 3 5 4 7 6
                // 4 5 3 2 1 7 6
                // 5 4 3 2 1 7 6

How to count number of 1's in a binary number ?

public static void cntBit(int n)
{
        int cnt = 0;
        while(n!=0)
        {
                if((n & 1) > 0)
                {
                        cnt++;
                }
               
                n = n>> 1;
        }
        System.out.println(cnt);
}

Java - Simple Text Editor

Problem Statement

you must implement a simple text editor. Initially, your editor contains an empty string, . You must perform operations of the following types:

    append - Append string to the end of .
    delete - Delete the last characters of .
    print - Print the character of .
    undo - Undo the last (not previously undone) operation of type or , reverting to the state it was in prior to that operation.

Input Format

The first line contains an integer, , denoting the number of operations.

Stack - Implementation

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    private static int[] stkArr = null;
       
        private static int[] stkMaxArr = null;
       
        private static int maxPointer = 0;
       
        private static int currentPointer =-1;
       
       
       
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                int N = sc.nextInt();
                stkArr = new int[N];
               
                stkMaxArr = new int[N];
               
                for(int i=0;i<N;i++)
                {
                        int m = sc.nextInt();
                       
                        if(m==1)
                        {

Breadth First Search - Java

Problem Statement

Given an undirected graph consisting of nodes (labelled 1 to N) where a specific given node represents the start position and an edge between any two nodes is of length units in the graph.

It is required to calculate the shortest distance from start position (Node S) to all of the other nodes in the graph.

Note 1: If a node is unreachable , the distance is assumed as .
Note 2: The length of each edge in the graph is units.

Input Format

The first line contains , denoting the number of test cases.

Sort lexicographically - Bigger is greater

Problem Statement:

Given a word , rearrange the letters of to construct another word in such a way that is lexicographically greater than . In case of multiple possible answers, find the lexicographically smallest one among them.

Input Format

The first line of input contains , the number of test cases. Each of the next lines contains .

Constraints


will contain only lower-case English letters and its length will not exceed .

Output Format