Saturday, July 20, 2024

Python Technical Interview Questions

 1)first non repeating character

Malayalam

y

programming

p



str = "MalayalaM"


a = '\0'

for c in str:

    if str.index(c) == str.rindex(c):

        a = c

        break


print(a)




2)



1

##

333

####

55555




for i in range(1, 6):

    for j in range(1, i+1):

        if i % 2 != 0:

            print(i, end='')

        else:

            print('#', end='')

    print()



3)


find two elements of array for given target sum


  a = {2, 11, 7, 15};

  target = 9;


output is


2,7



a = [2, 11, 7, 15]

target = 9

for i in range(len(a)):

 for j in range(i + 1, len(a)):

   if a[i] + a[j] == target:

    print(a[i],a[j])




4)union of two list



a=[1,2,3,4,5]

b=[3,2,6,7,8]



output

[1, 2, 3, 4, 5, 6, 7, 8]




a=[1,2,3,4,5]

b=[3,2,6,7,8]


set1=set(a)

set2=set(b)


set3=set1.union(set2)

print(set3)




5)intersection of two list


a=[1,2,3,4,5]

b=[3,2,6,7,8]



output


[2,3]



a=[1,2,3,4,5]

b=[3,2,6,7,8]


for i in range(len(a)):

    for j in range(i+1):

        if a[i] == b[j]:

            print(a[i])



6)Check if a String is a Palindrome


mam

reverse mam


a = "mam"

b = a[::-1]


if a == b:

    print("polindrome")

else:

    print("not polindrome")



7)remove duplicate in  name


input sabari


output sabri



a="sabari"

s=''.join(dict.fromkeys(a))

print(s)







Javascript Output Questions

 output questions


1.<!DOCTYPE html>

<html>

<body>


<button id="myButton">Click me</button>

<p id="output"></p>


<script>

document.getElementById("myButton").addEventListener("click", function() {

    document.getElementById("output").textContent = "Button was clicked!";

});

</script>


</body>

</html>



2.


let x = { b: 1, c: 2 }; 

let y = Object.keys(x); 

console.log(y.length); 


3.


let x = "5"; 

let y = 2; 


console.log(x + y); 

console.log(x - y); 



4.


let x = "hello"; 

let y = new String("hello"); 


console.log(x == y); 

console.log(x === y); 



5.


let x = 1; 

let y = "1"; 


console.log(++x, ++y); 



6.



let x = { a: 1, b: 2 }; 

let y = { b: 3 }; 

let z = { ...x, ...y }; 


console.log(z); 




7.


let x = { a: 1 }; 

let y = Object.assign({}, x); 


console.log(x === y); 




8.


let x = 5; 

let y = 6; 

x += x > y ? x : y; 


console.log(x); 



9.


let x = [1, 2, 3]; 

let y = x.map((x, i) => x + i); 


console.log(y); 



10.


let x = ["apple", "banana", "cherry"]; 

let y = x.filter((i) => i.startsWith("b")); 


console.log(y); 



11.


let x = 10.5; 

let y = parseInt(x); 


console.log(y); 



12.


let x = [1]; 

let y = x + 1; 


console.log(y); 



13.


let x = [1, 2, 3]; 

let y = x.slice(); 


console.log(y, x === y); 



14.



const a = [1, 2, 3]; 

const b = [...a]; 

b.push(4); 


console.log(a); 



15.


let a = [1, 2, 3]; 

let b = [4, 5, 6]; 


console.log(a + b); 



16.


let x = [1, 2, 3]; 


console.log(x.concat(4, 5)); 



17.


<!DOCTYPE html>

<html>

<body>


<ul id="myList">

  <li>Item 1</li>

  <li>Item 2</li>

</ul>


<script>

let newItem = document.createElement("li");

newItem.textContent = "Item 3";

document.getElementById("myList").appendChild(newItem);

</script>


</body>

</html>




18.


let c = { greeting: 'Hey!' };

let d;


d = c;

c.greeting = 'Hello';

console.log(d.greeting);


19.


let greeting;

greetign = {};

console.log(greetign);


20.


function sum(a, b) {

  return a + b;

}


sum(1, '2');



21.



let number = 0;

console.log(number++);

console.log(++number);

console.log(number);



22.



function getAge() {

  'use strict';

  age = 21;

  console.log(age);

}


getAge();




23.


const sum = eval('10*10+5');



24.



const obj = { a: 'one', b: 'two', a: 'three' };

console.log(obj);



25.


for (let i = 1; i < 5; i++) {

  if (i === 3) continue;

  console.log(i);

}


26.


<div onclick="console.log('div')">

  <p onclick="console.log('p')">

    Click here!

  </p>

</div>


27.



const firstPromise = new Promise((res, rej) => {

  setTimeout(res, 500, 'one');

});


const secondPromise = new Promise((res, rej) => {

  setTimeout(res, 100, 'two');

});


Promise.race([firstPromise, secondPromise]).then(res => console.log(res));



28.


let person = { name: 'Lydia' };

const members = [person];

person = null;


console.log(members);


29.



const person = {

  name: 'Lydia',

  age: 21,

};


for (const item in person) {

  console.log(item);

}


30.



const set = new Set([1, 1, 2, 3, 4]);


console.log(set);

Java Logical Interview Questions with Answers

 1)print the letters only in a string


Eg 1: Input: sabari10

output:sabari

program


import java.util.*;

public class Abc {


public static void main(String[] args) {

    String a,b="";

    int i;

    Scanner sc=new Scanner(System.in);

    a=sc.next();

    for(i=0;i<a.length();i++)

    {

    if(a.charAt(i)>='0' && a.charAt(i)<='9')

    {

    continue;

    }

    b+=a.charAt(i);

    }

    

    System.out.println(b);

    

    

}


}




2)integer to binary


input


10


output

1010


import java.util.*;

public class Abc {


public static void main(String[] args) {

    int a;

    Scanner sc=new Scanner(System.in);

    

    a=sc.nextInt();

    

    System.out.println(Integer.toBinaryString(a));

    

    

}


}



3)count the character  occurance in string


input

sab

output

s-1

a-1

b-1


import java.util.*;

public class Abc {


public static void main(String[] args) {

    String a;

    Scanner sc=new Scanner(System.in);

    int count=0;

    a=sc.next();

    char b[]=a.toCharArray();

    

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

    {

    for(int j=0;j<b.length;j++)

    {

    if(b[i]==b[j])

    {

    count++;

    }

    }

    System.out.println(b[i]+""+count);

    count=0;

    }

    

    

}


}


4)

array find the prime number


example


input

5


4 6 9 3 7


output 


3 7


import java.util.*;

public class Abc {


public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

    int n;

    n=sc.nextInt();

    int a[]=new int[n];

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

    {

    a[i]=sc.nextInt();

    }

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

    {

    if(a[i]%2!=0 && a[i]%3!=0 && a[i]%5!=0 && a[i]%7!=0 )

    {

    System.out.println(a[i]);

    }

    else if(a[i]==2 || a[i]==3 || a[i]==5 || a[i]==7 )

    {

    System.out.println(a[i]);

    }

    }

    

}


}



5)


Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8} 

Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8}


Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8} 

Output: arr3[] = {4, 5, 7, 8, 8, 9} 


import java.util.*;

public class Abc {


public static void main(String[] args) {

  int a[] = {1, 3, 5, 7};

  int b[] = {2, 4, 6, 8};

          

  int n1 = a.length;

      int n2 = b.length;

 

        int c[] = new int[n1 + n2];

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

        

        for(i=0;i<n1;i++)

        {

        c[k++]=a[i];

        }

        for(j=0;j<n2;j++)

        

        {

        c[k++]=b[j];

        }

        

        Arrays.sort(c);

        

            for(i=0;i<c.length;i++)

            {

            System.out.println(c[i]);

            }

    

    

    

}


}



6)


Oddly Even Problem Statement

Given a maximum of 100 digit numbers as input, find the difference between the sum of odd and even position digits

Test Cases

Case 1

Input: 4567

Expected Output: 2

Explanation : Odd positions are 4 and 6 as they are pos: 1 and pos: 3, both have sum 10. Similarly, 5 and 7 are at even positions pos: 2 and pos: 4 with sum 12. Thus, difference is 12 – 10 = 2

Case 2

Input: 5476

Expected Output: 2

Case 3

Input: 9834698765123

Expected Output: 1





import java.util.*;

public class Abc {


public static void main(String[] args) {

int a,c1=0,c2=0,pos=1;

Scanner sc=new Scanner(System.in);

a=sc.nextInt();

while(a!=0)

{

int b=a%10;

if(pos%2==0)

{

c1=c1+b;

}

else

{

c2=c2+b;

}

a=a/10;

pos++;

}

System.out.println(Math.abs(c1-c2));

}


}


Java Logical

 first largest number



import java.util.*;

class Abc {


public static void main(String[] args) {

int a[]= {10,20,44,1,5,8,99};

int max=0;

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

{

if(a[i]>max)

{

max=a[i];

}

}

System.out.println(max);

}


}



second largest number



import java.util.*;

class Abc {


public static void main(String[] args) {

int a[]= {10,20,44,1,5,8,99};

int max=0,smax=0;

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

{

if(a[i]>max)

{

max=a[i];

}

}

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

{

if(a[i]!=max)

{

smax=Math.max(smax,a[i]);

}

}

System.out.println(smax);

}


}




remove duplicate in string



import java.util.*;

class Abc {


public static void main(String[] args) {

String a="sabarii";

String b="";

int i;

for(i=0;i<a.length();i++)

{

char c=a.charAt(i);

if(b.indexOf(c)==-1)

{

b=b+c;

}


}

System.out.println(b);

}

}



biggest word in string




import java.util.*;

class Abc {


public static void main(String[] args) {

String s = "Today is the happiest day of my life";

String s1[]=s.split(" ");

int max=0;

int j=0;

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

{

if(s1[i].length()>max)

{

max=s1[i].length();

j=i;

}

}

System.out.println(s1[j]);




}





}



smallest word in string



import java.util.*;

class Abc {


public static void main(String[] args) {

String s = "Today is the happiest day of my life";

String s1[]=s.split(" ");

int min=s1[0].length();

int j=0;

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

{

if(s1[i].length()<min)

{

min=s1[i].length();

j=i;

}

}

System.out.println(s1[j]);




}





}





dead lock example



import java.util.*;

class Abc {


public static void main(String[] args) {

Object lock1 = new Object();

Object lock2= new Object();

  Thread thread1 = new Thread(() -> {

            synchronized (lock1) {

                System.out.println("Thread 1 acquired lock1");

                try {

                    Thread.sleep(100);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                synchronized (lock2) {

                    System.out.println("abc");

                }

            }

        });


        // Thread 2

        Thread thread2 = new Thread(() -> {

            synchronized (lock2) {

                System.out.println("Thread 2 acquired lock2");

                try {

                    Thread.sleep(100);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                synchronized (lock1) {

                    System.out.println("hello");

                }

            }

        });


        // Start the threads

        thread1.start();

        thread2.start();

}

}





multidimentional array swapping


import java.util.*;

class Abc {


public static void swap(int [][]t,int a, int b,int c,int d) {

    int temp = t[a][b];

    t[a][b] = t[c][d];

    t[c][d] = temp;

}

public static void main(String[] args) {

int[][] arr = {{1}, {2}, {3}};

swap(arr,0,0,1,0);

System.out.println(arr[0][0]);

    

}

}


Java Technical Interview Questions and Answers 2

1)


first non repeating character

Malayalam

y

programming

p



class HelloWorld {

    public static void main(String[] args) {

        String str = "MalayalaM";

       

        char a = '\0'; 

        for (char c : str.toCharArray()) {

            if (str.indexOf(c) == str.lastIndexOf(c)) {

                a = c;

                break;

            }

           

        }


        System.out.println(a);

        

    }

}


2)



1

##

333

####

55555



class HelloWorld {

    public static void main(String[] args) {

        for(int i=1;i<=5;i++)

        {

            for(int j=1;j<=i;j++)

            {

                if(i%2!=0)

                {

                    System.out.print(i);

                }

                else

                {

                    System.out.print("#");

                }

            }

            System.out.println();

        }

        

    }

}


3)


find two elements of array for given target sum





class HelloWorld {

    public static void main(String[] args) {

        int[] a = {2, 11, 7, 15};

        int target = 9;

        

       

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

            for (int j = i + 1; j < a.length; j++) {

                if (a[i] + a[j] == target) {

                System.out.println(a[i] + ", " + a[j]);

                }

            }

        }

        

    }

}




4)

find union of two arrays



int a[]={1,2,3,4,5};

int b[]={3,2,6,7,8};



output


[1, 2, 3, 4, 5, 6, 7, 8]



import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

        int a[]={1,2,3,4,5};

int b[]={3,2,6,7,8};

HashSet<Integer> s=new HashSet<Integer>();

for(int i=0;i<5;i++)

{

    s.add(a[i]);

}

for(int i=0;i<5;i++)

{

    s.add(b[i]);

}

        System.out.println(s);

        

    }

}



5)


find intersection of two arrays



int a[]={1,2,3,4,5};

int b[]={3,2,6,7,8};



output


{2,3}



import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

        int a[]={1,2,3,4,5};

int b[]={3,2,6,7,8};


for(int i=0;i<5;i++)

{

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

    {

        if(a[i]==b[j])

        {

            System.out.println(a[i]);

        }

    }

}



    }

}



6)Check if a String is a Palindrome


import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

       String a="mam";

       StringBuilder b = new StringBuilder(a);

        b.reverse();


    if(a.equals(b.toString()))

    {

        System.out.println("polindrome");

    }

    else

    {

        System.out.println("not polindrome");

    }

    }

}



7)remove duplicate in array


import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

    String a="sabari";

    char c[]=a.toCharArray();

    LinkedHashSet<Character> d=new LinkedHashSet<Character>();

    for(char c1:c)

    {

        d.add(c1);

    }

    StringBuilder sb = new StringBuilder();

for (Character c2 : d) {

    sb.append(c2);

}

System.out.println(sb);

   

    

    }

}




8)

Given an array A[] and a number x, check for pair in A[] with sum as x.


Input

 {2, 11, 7,3,6, 15}


output


2, 7

3, 6

total count 2




9)

 first occurrence of a string in another string


 a = "Hello, how are you?";

 b = "how";


First occurrence of how is at index: 7



import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

  String a = "Hello, how are you?";

String b = "how";


int index = a.indexOf(b);


if (index != -1) {

    System.out.println("First occurrence of "+ b +" is at index: " + index);

} else {

    System.out.println(" not found.");

}


   

    

    }

}




10)Shuffle array


import java.util.*;

class HelloWorld {

    public static void main(String[] args) {


int a[]={1,2,3,4,5,6};

 ArrayList<Integer> al=new ArrayList<Integer>();

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

 {

 al.add(a[i]);

 }

Collections.shuffle(al);


 System.out.println(al);

 

    

    }

}



11)Number of occurrence



X = 2

Arr[] = {1, 1, 2, 2, 2, 2, 3}

Output: 4

Explanation: 2 occurs 4 



import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

int a[] = {1, 1, 2, 2, 2, 2, 3};

int x=2,count=0;


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

{

    if(a[i]==x)

    {

        count++;

    }

}

 

 System.out.println(x+" occurs "+count);

    

    }

}



Input:



use abc;

CREATE TABLE Worker (

WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

FIRST_NAME CHAR(25),

LAST_NAME CHAR(25),

SALARY INT(15),

JOINING_DATE DATETIME,

DEPARTMENT CHAR(25)

);


INSERT INTO Worker 

(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT) VALUES

(001, 'Monika', 'Arora', 100000, '21-02-20 09.00.00', 'HR'),

(002, 'Niharika', 'Verma', 80000, '21-06-11 09.00.00', 'Admin'),

(003, 'Vishal', 'Singhal', 300000, '21-02-20 09.00.00', 'HR'),

(004, 'Amitabh', 'Singh', 500000, '21-02-20 09.00.00', 'Admin'),

(005, 'Vivek', 'Bhati', 500000, '21-06-11 09.00.00', 'Admin'),

(006, 'Vipul', 'Diwan', 200000, '21-06-11 09.00.00', 'Account'),

(007, 'Satish', 'Kumar', 75000, '21-01-20 09.00.00', 'Account'),

(008, 'Geetika', 'Chauhan', 90000, '21-04-11 09.00.00', 'Admin');






Q. Write a SQL query to find the 3th highest salary from employee table?



SELECT DISTINCT salary

FROM worker

ORDER BY salary DESC

LIMIT 1 OFFSET 2;





Q. SQL queries

-- 2> Write a SQL query to find top n records?

-- Example: finding top 5 records from employee table



SELECT *

FROM worker

ORDER BY salary DESC

LIMIT 5;





-- 3> Write a SQL query to find the count of employees working in department 'Admin'


select count(*) from worker where department='Admin'


-- 4> Write a SQL query to fetch department wise count employees sorted by department count in desc order.



SELECT department, COUNT(*) AS empcount

FROM worker

GROUP BY department

ORDER BY empcount DESC;






-- 5> Write a SQL query to find only odd rows from employee table




SELECT *

FROM worker

WHERE worker_id % 2 <> 0;




6> Write a SQL query to show the last record from a table.


SELECT *

FROM worker

order by worker_id desc limit 1;





-- 7> Write a SQL query to show the first record from a table.


SELECT *

FROM worker

 limit 1;



-- 8> Write a SQL query to get last five records from a employee table.


SELECT *

FROM worker

order by worker_id desc limit 5;




-- 9> Write a SQL query to find employees having the highest salary in each department. 


select * from worker where salary in (select max(salary) from worker group by department )




-- 10> Write a SQL query to fetch departments along with the total salaries paid for each of them.


select department,sum(salary) from worker group by department





-- 11> write a SQL query to find employees with same salary



SELECT e1.*

FROM worker e1

JOIN worker e2 ON e1.salary = e2.salary

WHERE e1.worker_id!=e2.worker_id;














 

TCS Java Technical Interview Questions and Answers 1

 1)Accept two positive integers M and N as input. There are two cases to consider:

(1) If M < N, then print M as output.

(2) If M >= N, subtract N from M. Call the difference M1. If M1 >= N, then subtract N from M1 and call the difference M2. Keep doing this operation until you reach a value k, such that, Mk < N. You have to print the value of Mk as output.

import java.util.Scanner;


public class CodeTranslation {

    public static int get(int M, int N) {

        if (M < N) {

            return M;

        } else {

            int Mk = M;

            while (Mk >= N) {

                Mk -= N;

            }

            return Mk;

        }

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int M = scanner.nextInt();

        int N = scanner.nextInt();

        int output = get(M, N);

        System.out.println(output);

    }

}




2)Accept three positive integers as input and check if they form the sides of a right triangle.



 Print YES if they form one, and NO if they do not. The input will have three lines, with one integer on each line. The output should be a single line containing one of these two strings: YES or NO



(Hypotenuse)2 = (Base)2 + (Perpendicular)2 






import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int x = scanner.nextInt();

        int y = scanner.nextInt();

        int z = scanner.nextInt();


        if ((Math.pow(x, 2) + Math.pow(y, 2) == Math.pow(z, 2)) ||

            (Math.pow(y, 2) + Math.pow(z, 2) == Math.pow(x, 2)) ||

            (Math.pow(z, 2) + Math.pow(x, 2) == Math.pow(y, 2))) {

            System.out.print("YES");

        } else {

            System.out.print("NO");

        }

    }

}


3)

Accept a string as input. Your task is to determine if the input string is a valid password or not. For a string to be a valid password, it must satisfy all the conditions given below:

(1) It should have at least 8 and at most 32 characters

(2) It should start with an uppercase or lowercase letter

(3) It should not have any of these characters: / \ = ' "

(4) It should not have spaces

It could have any character that is not mentioned in the list of characters to be avoided (points 3 and 4). Output True if the string forms a valid password and False otherwise.




import java.util.Scanner;


public class PasswordValidator {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String password = scanner.nextLine().trim();


        boolean a = true;


        if (!(password.length() >= 8 && password.length() <= 32)) {

            a = false;

        } else {

           

           char firstChar = password.charAt(0);

            if (!(('a' <= firstChar && firstChar <= 'z') || ('A' <= firstChar && firstChar <= 'Z'))) {

                a = false;

            } else {

               


                String s = "/\\='\\"";

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

                    if (s.contains(String.valueOf(password.charAt(i)))) {

                        a = false;

                        break;

                    }

                }

                if (a) {

                   

                  if (password.contains(" ")) {

                        a = false;

                    }

                }

            }

        }


        System.out.println(a);

    }

}







5)Accept a string as input, convert it to lower case, sort the string in alphabetical order, and print the sorted string to the console. You can assume that the string will only contain letters.



import java.util.*;


public class code {


    public static void main(String[] args) {

        Scanner sc = new java.util.Scanner(System.in);

        String c = sc.nextLine();

        c = c.toLowerCase();

        char[] charArray = c.toCharArray();

        Arrays.sort(charArray);

        String b = new String(charArray);

        System.out.println(b);

        

    }

}



Python Technical Interview Questions

 1)first non repeating character Malayalam y programming p str = "MalayalaM" a = '\0' for c in str:     if str.index(c) ==...