Category: Coding Interview Question

Coding Interview Question How to swap two numbers without using third variable?

How to swap two numbers without using third variable?

  public class MySwapingTwoNumbers {     public static void main(String a[]){         int x = 10;         int y = 20;         System.out.println(“Before swap:”);         System.out.println(“x value: “+x);         System.out.println(“y value: “+y);         x = x+y;         y=x-y;         x=x-y;         System.out.println(“After swap:”);         System.out.println(“x value: “+x);         System.out.println(“y value: “+y);     } } Output: Before swap: x value: 10 y value: 20 After swap: x value: 20 …

Read Article Read More

Coding Interview Question Factorial Program

Factorial Program

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example: 4! = 4*3*2*1 = 24 5! = 5*4*3*2*1 = 120 Code for Factorial class FactorialExample{ public static void main(String args[]){ int i,fact=1; int number=5;//It is the number to calculate factorial for(i=1;i<=number;i++){ fact=fact*i; } System.out.println(“Factorial of …

Read Article Read More

Coding Interview Question Prime number

Prime number

Prime number is a number that is greater than 1 and divided by 1 or itself.  Prime numbers can’t be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17…. are the prime numbers. Code for Prime Number class PrimeExample{ public static void main(String args[]){ int i,m=0,flag=0; int n=11;//it …

Read Article Read More

Coding Interview Question Fibonacci series

Fibonacci series

Fibonacci series is the series of those numbers which are the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of Fibonacci series are 0 and 1. class FibonacciSeries{ public static void main(String args[]) { int num1=0,num2=1,num3,i,count=10; for(i=2;i<count;++i)//loop starts from 2 …

Read Article Read More

Coding Interview Question Reverse an array in subset of N

Reverse an array in subset of N

  private int[] reverseArray(int[] A, int k) { for (int i = 0; i < A.length; i += k) { int left = i; // in case right larger than A.length int right = Math.min(i + k – 1, A.length – 1); // reverse sub array while (left < right) { int temp = A[left]; …

Read Article Read More