fa
Feedback
Tech Jargon - Decoded

Tech Jargon - Decoded

رفتن به کانال در Telegram

Confused by tech terms? Don’t worry, we’ve got you 🤝 We make things simple, one concept at a time. Learn daily Easy & clear Turn Confusion into clarity. #tech #it #softwareengineer #cs #development

نمایش بیشتر
2 015
مشترکین
-124 ساعت
-57 روز
-4030 روز
آرشیو پست ها
#Java #StringReverse #Iterative #Recursive #Interview

Reverse a String (Iterative & Recursive)
public class StringReversal {

    public static String reverseIterative(String str) {
        StringBuilder reversed = new StringBuilder();
        for (int i = str.length() - 1; i >= 0; i--) {
            reversed.append(str.charAt(i));
        }
        return reversed.toString();
    }

    public static String reverseRecursive(String str) {
        if (str.isEmpty()) {
            return str;
        } else {
            return reverseRecursive(str.substring(1)) + str.charAt(0);
        }
    }

    public static void main(String[] args) {
        String input = "hello";
        String reversedIterative = reverseIterative(input);
        String reversedRecursive = reverseRecursive(input);
        System.out.println("Iterative: " + reversedIterative);
        System.out.println("Recursive: " + reversedRecursive);
    }
}

#Java #Primes #Sieve

Count Primes Less than N
public class CountPrimes {
    public int countPrimes(int n) {
        if (n <= 2) {
            return 0;
        }
        boolean[] isPrime = new boolean[n];
        for (int i = 2; i < n; i++) {
            isPrime[i] = true;
        }
        for (int i = 2; i * i < n; i++) {
            if (!isPrime[i]) {
                continue;
            }
            for (int j = i * i; j < n; j += i) {
                isPrime[j] = false;
            }
        }
        int count = 0;
        for (int i = 2; i < n; i++) {
            if (isPrime[i]) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        CountPrimes cp = new CountPrimes();
        int n = 10;
        int primeCount = cp.countPrimes(n);
        System.out.println("Number of primes less than " + n + " is: " + primeCount);
    }
}

#Java #Primes #Algorithm

Count Primes Less than N
public class CountPrimes {
    public int countPrimes(int n) {
        if (n <= 2) return 0;
        boolean[] isPrime = new boolean[n];
        for (int i = 2; i < n; i++) {
            isPrime[i] = true;
        }
        for (int i = 2; i * i < n; i++) {
            if (!isPrime[i]) continue;
            for (int j = i * i; j < n; j += i) {
                isPrime[j] = false;
            }
        }
        int count = 0;
        for (int i = 2; i < n; i++) {
            if (isPrime[i]) count++;
        }
        return count;
    }

    public static void main(String[] args) {
        CountPrimes cp = new CountPrimes();
        int n = 10;
        int result = cp.countPrimes(n);
        System.out.println("Number of primes less than " + n + " is: " + result);
    }
}

#Java #Fibonacci #MatrixExponentiation

Fast Fibonacci using Matrix Exponentiation
public class Fibonacci {

    public static long fibonacci(int n) {
        if (n <= 1) {
            return n;
        }
        long[][] baseMatrix = {{1, 1}, {1, 0}};
        long[][] resultMatrix = matrixPower(baseMatrix, n - 1);
        return resultMatrix[0][0];
    }

    private static long[][] matrixMultiply(long[][] a, long[][] b) {
        long[][] result = new long[2][2];
        result[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0];
        result[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1];
        result[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0];
        result[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1];
        return result;
    }

    private static long[][] matrixPower(long[][] base, int n) {
        long[][] result = {{1, 0}, {0, 1}};
        long[][] temp = base;
        while (n > 0) {
            if ((n & 1) == 1) {
                result = matrixMultiply(result, temp);
            }
            temp = matrixMultiply(temp, temp);
            n >>= 1;
        }
        return result;
    }

    public static void main(String[] args) {
        int n = 10;
        System.out.println("Fibonacci(" + n + ") = " + fibonacci(n));
    }
}

#Java #PrimeNumbers #SieveOfEratosthenes

Count Primes Less than N
public class CountPrimes {

    public int countPrimes(int n) {
        if (n <= 2) {
            return 0;
        }

        boolean[] isPrime = new boolean[n];
        for (int i = 2; i < n; i++) {
            isPrime[i] = true;
        }

        for (int i = 2; i * i < n; i++) {
            if (!isPrime[i]) continue;
            for (int j = i * i; j < n; j += i) {\n                isPrime[j] = false;
            }
        }

        int count = 0;
        for (int i = 2; i < n; i++) {
            if (isPrime[i]) count++;
        }

        return count;
    }

    public static void main(String[] args) {
        CountPrimes cp = new CountPrimes();
        int n = 10;
        int primeCount = cp.countPrimes(n);
        System.out.println("Number of primes less than " + n + " is: " + primeCount);
    }
}

#Java #Fibonacci #MatrixExponentiation

Fast Fibonacci using Matrix Exponentiation
public class Fibonacci {    public static long fibonacci(int n) {        if (n <= 1) return n;        long[][] matrix = {{1, 1}, {1, 0}};        power(matrix, n - 1);        return matrix[0][0];    }    private static void power(long[][] matrix, int n) {        if (n == 0 || n == 1) return;        long[][] temp = {{1, 1}, {1, 0}};        power(matrix, n / 2);        multiply(matrix, matrix);        if (n % 2 != 0) multiply(matrix, temp);    }    private static void multiply(long[][] a, long[][] b) {        long a00 = a[0][0], a01 = a[0][1], a10 = a[1][0], a11 = a[1][1];        long b00 = b[0][0], b01 = b[0][1], b10 = b[1][0], b11 = b[1][1];        a[0][0] = (a00 * b00 + a01 * b10);        a[0][1] = (a00 * b01 + a01 * b11);        a[1][0] = (a10 * b00 + a11 * b10);        a[1][1] = (a10 * b01 + a11 * b11);    }    public static void main(String[] args) {        int n = 10;        System.out.println("Fibonacci(" + n + ") = " + fibonacci(n));    }}

#Java #PrimeNumbers #Algorithm

Count Primes Less than N
public class CountPrimes {
    public int countPrimes(int n) {
        if (n <= 2) return 0;

        boolean[] isPrime = new boolean[n];
        for (int i = 2; i < n; i++) {
            isPrime[i] = true;
        }

        for (int i = 2; i * i < n; i++) {
            if (!isPrime[i]) continue;
            for (int j = i * i; j < n; j += i) {
                isPrime[j] = false;
            }
        }

        int count = 0;
        for (int i = 2; i < n; i++) {
            if (isPrime[i]) count++;
        }

        return count;
    }

    public static void main(String[] args) {
        CountPrimes cp = new CountPrimes();
        int n = 10;
        int primeCount = cp.countPrimes(n);
        System.out.println("Number of primes less than " + n + ": " + primeCount);
    }
}

Check Palindrome String (Ignore Case)

#Java #StringReverse #Recursion

Reverse a String (Iterative & Recursive)
public class StringReversal {

    public static String reverseIterative(String str) {
        StringBuilder reversed = new StringBuilder();
        for (int i = str.length() - 1; i >= 0; i--) {
            reversed.append(str.charAt(i));
        }
        return reversed.toString();
    }

    public static String reverseRecursive(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        return reverseRecursive(str.substring(1)) + str.charAt(0);
    }

    public static void main(String[] args) {
        String input = "hello";
        String reversedIterative = reverseIterative(input);
        String reversedRecursive = reverseRecursive(input);
        System.out.println("Iterative: " + reversedIterative);
        System.out.println("Recursive: " + reversedRecursive);
    }
}

#Java #PrimeNumbers #Algorithm

Count Primes Less than N
public class PrimeCounter {
    public static int countPrimes(int n) {
        if (n <= 2) {
            return 0;
        }

        boolean[] isPrime = new boolean[n];
        for (int i = 2; i < n; i++) {
            isPrime[i] = true;
        }

        for (int i = 2; i * i < n; i++) {
            if (!isPrime[i]) {
                continue;
            }
            for (int j = i * i; j < n; j += i) {
                isPrime[j] = false;
            }
        }

        int count = 0;
        for (int i = 2; i < n; i++) {
            if (isPrime[i]) {
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        int n = 10;
        int primeCount = countPrimes(n);
        System.out.println("Number of primes less than " + n + ": " + primeCount);
    }
}

#Java #Fibonacci #MatrixExponentiation