A Way For Learning

Interview Questions and Answers

No comments
Interview Preparation - Shell Scripting, Java, Autosys, SDLC

Table of Contents


Shell Scripts

1. Find Second Largest + Second Smallest and Add Them


#!/bin/bash
arr=(5 1 9 6 1 2 8)
sorted=($(printf "%s\n" "${arr[@]}" | sort -n | uniq))
second_smallest=${sorted[1]}
second_largest=${sorted[-2]}
sum=$((second_smallest + second_largest))
echo "Second Smallest: $second_smallest"
echo "Second Largest: $second_largest"
echo "Sum: $sum"

2. Find Unique Characters and Longest Substring Without Repetition


#!/bin/bash
input="abcabcbb"
longest=""
temp=""
for (( i=0; i<${#input}; i++ )); do
    char="${input:i:1}"
    if [[ "$temp" == *"$char"* ]]; then
        temp="${temp#*$char}${char}"
    else
        temp="$temp$char"
    fi
    if [ ${#temp} -gt ${#longest} ]; then
        longest="$temp"
    fi
done
echo "Longest substring without repetition: $longest"

3. Reverse a String with Tests


reverseStr() {
  local str="$1"
  echo "$str" | rev
}

doTestsPass() {
  local result=0
  if [[ "$(reverseStr 'abcd')" == "dcba" ]]; then result=$((result + 1)); fi
  if [[ "$(reverseStr 'odd abcde')" == "edcba ddo" ]]; then result=$((result + 1)); fi
  if [[ "$(reverseStr 'even abcde')" == "edcba neve" ]]; then result=$((result + 1)); fi
  if [[ "$(reverseStr "$(reverseStr 'no change')")" == "no change" ]]; then result=$((result + 1)); fi
  if [[ "$(reverseStr '')" == "" ]]; then result=$((result + 1)); fi

  if [[ $result -eq 5 ]]; then
    echo "All tests pass"
  else
    echo "There are test failures"
  fi
}
doTestsPass

4. Print Line 50 to 60


sed -n '50,60p' filename.txt

5. Search Pattern and Count Occurrence


grep -o "pattern" filename.txt | wc -l

6. Find Dot Product of Two Arrays


#!/bin/bash
a=(1 2 3)
b=(4 5 6)
dot_product=0
for i in "${!a[@]}"; do
  dot_product=$((dot_product + a[i]*b[i]))
done
echo "Dot Product: $dot_product"

7. Check and Print Integers from a String


#!/bin/bash
str="abc123def45gh6"
echo "$str" | grep -o '[0-9]\+'

8. Find 3rd Least Array Value


#!/bin/bash
arr=(5 2 8 1 7 9)
sorted=($(printf "%s\n" "${arr[@]}" | sort -n | uniq))
echo "Third Least: ${sorted[2]}"

9. Check Process Status


#!/bin/bash
process="sshd"
if pgrep "$process" > /dev/null
then
  echo "$process is running"
else
  echo "$process is not running"
fi

10. Delete Blank Lines


sed -i '/^$/d' filename.txt

11. Lines Greater Than 5 Characters (awk)


awk 'length($0) > 5' filename.txt

12. Print Last 10 Lines


tail -n 10 filename.txt

13. Check Palindrome


#!/bin/bash
read -p "Enter a string: " str
rev_str=$(echo "$str" | rev)
if [[ "$str" == "$rev_str" ]]; then
    echo "Palindrome"
else
    echo "Not Palindrome"
fi

14. Check File Exists, Size, Send Email


#!/bin/bash
file="yourfile.txt"
if [[ -f "$file" ]]; then
    size=$(stat -c%s "$file")
    echo "File size: $size bytes"
    if (( size > 0 )); then
        echo "File exists and is non-empty" | mail -s "File Status" your@email.com
    else
        echo "File is empty" | mail -s "File Empty Alert" your@email.com
    fi
else
    echo "File not found" | mail -s "File Not Found" your@email.com
fi

AutoSys & Job Scheduling

On Hold vs On Ice

  • On Hold: Job is manually stopped. Needs manual release.
  • On Ice: Job is skipped this time but will run in next schedule automatically.

Predecessor and Successor

  • Predecessor: Job that must complete first.
  • Successor: Job that runs after predecessor finishes.

Job Schedule Example (Cron)


0 10 * * 1-5 /path/to/your/script.sh
# Runs Monday to Friday at 10 AM

Migration Testing Points

  • Validate job dependencies.
  • Check environment variables.
  • Check file watchers, time triggers.
  • Validate calendars, schedules.
  • Test job logs and outputs.
  • Test email alerts.

Java Programming

Kill Process Tree


import java.util.*;
public class KillProcess {
    public static void main(String[] args) {
        int[] pid = {1, 3, 10, 5};
        int[] ppid = {3, 0, 5, 3};
        int kill = 5;
        List result = new ArrayList<>();
        Map> map = new HashMap<>();
        for (int i = 0; i < ppid.length; i++) {
            map.computeIfAbsent(ppid[i], k -> new ArrayList<>()).add(pid[i]);
        }
        Queue queue = new LinkedList<>();
        queue.add(kill);
        while (!queue.isEmpty()) {
            int curr = queue.poll();
            result.add(curr);
            if (map.containsKey(curr)) {
                queue.addAll(map.get(curr));
            }
        }
        System.out.println(result);
    }
}

Find Odd and Even Numbers


int[] arr = {2,3,4,5,6,3,7};
for(int i=0; i

Character Frequency Count


String str = "abcdascgab";
Map map = new HashMap<>();
for(char c : str.toCharArray()) {
    map.put(c, map.getOrDefault(c,0)+1);
}
map.forEach((k,v) -> System.out.println(k + " -> " + v));

Longest Occurrence of Character


public class LongestChar {
    public static void main(String[] args) {
        String s = "aaabbccccdde";
        int maxLen = 0, currLen = 1, start = 0, maxStart = 0;
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == s.charAt(i-1)) {
                currLen++;
            } else {
                if (currLen > maxLen) {
                    maxLen = currLen;
                    maxStart = i - currLen;
                }
                currLen = 1;
            }
        }
        if (currLen > maxLen) {
            maxLen = currLen;
            maxStart = s.length() - currLen;
        }
        System.out.println("Character: " + s.charAt(maxStart));
        System.out.println("Starting Index: " + maxStart);
        System.out.println("Length: " + maxLen);
    }
}

Python Programs

Find Missing Characters to Form Pangram


import string
def missing_chars(input_str):
    input_str = input_str.lower()
    missing = [ch for ch in string.ascii_lowercase if ch not in input_str]
    return ''.join(missing)
print(missing_chars("the quick brown fox jumps over the dog"))

First Non-Repeating Character


def first_unique_char(s):
    for c in s:
        if s.count(c) == 1:
            return c
    return None
print(first_unique_char("apple"))

Unix, Git, CI/CD, SDLC Knowledge

Unix Commands

  • cd: change directory
  • pwd: print working directory
  • ls: list files
  • mkdir: create directory
  • rm: remove files
  • sed: text manipulation
  • awk: pattern scanning and processing

CI/CD Pipeline Flow

  • Code Checkout ➔ Build ➔ Test ➔ Deploy ➔ Monitor
  • Tools: Git, Jenkins, Nexus, SonarQube

SDLC Phases

  • Planning
  • Analysis
  • Design
  • Development
  • Testing
  • Deployment
  • Maintenance

Git Rebase

Move branch commits to a new base, to make history linear.

Merge Conflict Resolution

  • Pull changes.
  • Edit conflicts manually.
  • Mark resolved.
  • Commit.

No comments :

Post a Comment