├── OS_InterviewBit.pdf ├── Microsoft ├── Leetcode-Microsoft │ └── Microsoft - LeetCode.pdf ├── InterviewBit │ ├── Easy │ │ ├── reverse_string.java │ │ ├── merge_two_sorted_ll.java │ │ └── majority_element.java │ └── Medium │ │ ├── distribute_candies.java │ │ ├── gas_station.java │ │ └── longest_increasing_subsequence.java ├── stocks_part1.java ├── anagram_difference.java ├── StringDivisbleby3.java ├── key_pair.java ├── MissingNo.java ├── reverse_vowels.java ├── non_overlapping_intervals.java ├── longest_repeating_subsequence.java ├── stocks_part6.java ├── merge_intervals.java ├── meeting_room_1.java ├── stocks_part5.java ├── longest_length_palindromic_String.java ├── merge_two_sorted_arrays.java ├── stocks_part3.java ├── stocks_part4.java ├── count_palindromic_substrings.java └── stocks_part2.java ├── Top75 ├── DP │ ├── house_robber.java │ ├── climbing_stairs.java │ ├── coin_change.java │ └── max_sum_of_non_adjacent.java ├── arrays │ ├── longest_consecutive_sequence.java │ ├── find_min_in_rotated_sorted_array.java │ ├── search_in_rotated_sorted_array.java │ └── ThreeSum.java ├── stacks │ ├── stock_span.java │ └── largest_area_histogram.java ├── LinkedLists │ ├── reverse_nodes_in_k_group.java │ ├── copy_list_with_random_pointer.java │ └── merge_k_sorted_LL.java └── strings │ └── longest_string_without_repeating_characters.java ├── MorganStanley └── lis.java └── README.md /OS_InterviewBit.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewithjaspreet/Company-Wise/HEAD/OS_InterviewBit.pdf -------------------------------------------------------------------------------- /Microsoft/Leetcode-Microsoft/Microsoft - LeetCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewithjaspreet/Company-Wise/HEAD/Microsoft/Leetcode-Microsoft/Microsoft - LeetCode.pdf -------------------------------------------------------------------------------- /Microsoft/InterviewBit/Easy/reverse_string.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String solve(String A) { 3 | A = A.replaceAll("( )+", " "); 4 | A = A.trim(); 5 | String[] str = A.split(" "); 6 | String result =""; 7 | for(int i= str.length-1;i>=0;i--) 8 | result = result +" " +str[i]; 9 | return result.trim(); 10 | } 11 | } -------------------------------------------------------------------------------- /Top75/DP/house_robber.java: -------------------------------------------------------------------------------- 1 | package Top75.DP; 2 | 3 | class Solution { 4 | public int rob(int[] nums) { 5 | 6 | int[] dp = new int[nums.length + 1]; 7 | 8 | dp[0] = 0; 9 | 10 | dp[1] = Math.max(dp[0], nums[0]); 11 | 12 | for (int i = 2; i <= nums.length; i++) { 13 | 14 | dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]); 15 | } 16 | 17 | return dp[nums.length]; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Microsoft/stocks_part1.java: -------------------------------------------------------------------------------- 1 | public class stocks_part1 { 2 | public int maxProfit(int[] prices) { 3 | 4 | int profit = 0; 5 | int min = prices[0]; 6 | 7 | for (int i = 1; i < prices.length; i++) { 8 | 9 | int cost = prices[i] - min; 10 | profit = Math.max(profit, cost); 11 | min = Math.min(min, prices[i]); 12 | } 13 | 14 | return profit; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Microsoft/anagram_difference.java: -------------------------------------------------------------------------------- 1 | package Microsoft; 2 | 3 | public class anagram_difference { 4 | public static int getMinimumAnagramDifference(String str1, String str2) { 5 | // Write your code here. 6 | 7 | int[] arr = new int[26]; 8 | 9 | for (char c : str1.toCharArray()) 10 | arr[c - 'a']++; 11 | 12 | for (char c : str2.toCharArray()) 13 | arr[c - 'a']--; 14 | 15 | int c = 0; 16 | 17 | for (int a : arr) { 18 | 19 | if (a > 0) 20 | c += a; 21 | } 22 | 23 | return c; 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Microsoft/StringDivisbleby3.java: -------------------------------------------------------------------------------- 1 | package Microsoft; 2 | 3 | public class StringDivisbleby3 { 4 | 5 | // a string is divisible by 3 if the difference between the sum of odd and even digits is divisible by 3 6 | 7 | static int isDivisible(String str) { 8 | 9 | int odd = 0; 10 | 11 | int even = 0; 12 | 13 | for (int i = 0; i < str.length(); i++) { 14 | 15 | if (i % 2 == 0) 16 | even += str.charAt(i) - '0'; 17 | else 18 | odd += str.charAt(i) - '0'; 19 | } 20 | 21 | return (Math.abs(odd - even) % 3 == 0) ? 1 : 0; 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Microsoft/key_pair.java: -------------------------------------------------------------------------------- 1 | package Microsoft; 2 | import java.util.*; 3 | public class key_pair { 4 | 5 | public int[] twoSum(int nums[], int target) { 6 | 7 | 8 | HashMap map = new HashMap<>(); 9 | 10 | int[] arr = new int[2]; 11 | 12 | for(int i = 0 ; i < nums.length ; i ++){ 13 | 14 | if(map.containsKey(target - nums[i])){ 15 | 16 | arr[0] = i; 17 | arr[1] = map.get(target - nums[i]); 18 | return arr; 19 | } 20 | 21 | map.put(nums[i] , i); 22 | } 23 | 24 | return new int[]{}; 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Microsoft/MissingNo.java: -------------------------------------------------------------------------------- 1 | package Microsoft; 2 | 3 | public class MissingNo { 4 | 5 | // Given an array of size n-1 and given that there are numbers from 1 to n with one missing, 6 | // the missing number is to be found. 7 | // algorithm: sum of n natural numbers = n(n+1)/2 8 | // sum of array elements = sum 9 | // missing number = n(n+1)/2 - sum 10 | 11 | int MissingNumber(int array[], int n) { 12 | // Your Code Here 13 | 14 | int sum = 0; 15 | 16 | for (int a : array) { 17 | 18 | sum += a; 19 | } 20 | 21 | return (n * (n + 1)) / 2 - sum; 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Top75/DP/climbing_stairs.java: -------------------------------------------------------------------------------- 1 | package Top75.DP; 2 | 3 | class Solution { 4 | 5 | public int climbStairsRecursive(int n ){ 6 | 7 | 8 | if(n == 0) return 1; 9 | if(n < 0) return 0; 10 | 11 | return climbStairsRecursive(n-1) + climbStairsRecursive(n-2); 12 | } 13 | 14 | 15 | public int climbStairs(int n) { 16 | 17 | int[] dp = new int[n + 1]; 18 | dp[0] = 1; 19 | 20 | for (int i = 1; i <= n; i++) { 21 | if (i == 1) { 22 | dp[i] = dp[i - 1]; 23 | } else { 24 | dp[i] = dp[i - 1] + dp[i - 2]; 25 | } 26 | 27 | } 28 | return dp[n]; 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Microsoft/reverse_vowels.java: -------------------------------------------------------------------------------- 1 | 2 | // Reverse Vowels of a String 3 | // two pointers - awesome approach 4 | class Solution { 5 | 6 | public String reverseVowels(String s) { 7 | 8 | if(s == null || s.length()==0) return s; 9 | 10 | int i = 0 ; int j = s.length() - 1; 11 | 12 | String vowels = "aeiouAEIOU"; 13 | 14 | char[] temp = s.toCharArray(); 15 | 16 | while(i < j){ 17 | 18 | 19 | while(i < j && !vowels.contains(temp[i] + "")) i++; 20 | 21 | while(i < j && !vowels.contains(temp[j] +"")) j--; 22 | 23 | char cur = temp[i]; 24 | temp[i] = temp[j]; 25 | temp[j] = cur; 26 | 27 | i++; 28 | j--; 29 | } 30 | 31 | return new String(temp); 32 | 33 | 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Microsoft/InterviewBit/Medium/distribute_candies.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int candy(int[] ratings) { 3 | int sum=0; 4 | int[] a=new int[ratings.length]; 5 | for(int i=0;iratings[i]) 12 | { 13 | a[i+1]=a[i]+1; 14 | } 15 | } 16 | for(int i=ratings.length-1;i>0;i--) 17 | { 18 | if(ratings[i-1]>ratings[i]) 19 | { 20 | if(a[i-1]<(a[i]+1)) 21 | { 22 | a[i-1]=a[i]+1; 23 | } 24 | } 25 | } 26 | for(int i=0;i nums[prev]) { 28 | 29 | take = 1 + solve(cur + 1, cur, nums, dp); 30 | } 31 | 32 | int notTake = solve(cur + 1, prev, nums, dp); 33 | 34 | return dp[cur][prev + 1] = Math.max(take, notTake); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Top75/arrays/longest_consecutive_sequence.java: -------------------------------------------------------------------------------- 1 | package Top75.arrays; 2 | import java.util.HashMap; 3 | 4 | class Solution { 5 | public int longestConsecutive(int[] nums) { 6 | 7 | HashMap map = new HashMap<>(); 8 | 9 | for (int a : nums) { 10 | map.put(a, true); 11 | } 12 | 13 | for (int a : nums) { 14 | if (map.containsKey(a - 1)) { 15 | map.put(a, false); 16 | } 17 | } 18 | 19 | int msp = 0; 20 | int ml = 0; 21 | for (int a : nums) { 22 | if (map.get(a) == true) { 23 | int tl = 1; 24 | int tsp = a; 25 | while (map.containsKey(tsp + tl)) { 26 | tl++; 27 | } 28 | 29 | msp = Math.max(msp, tsp); 30 | ml = Math.max(ml, tl); 31 | 32 | } 33 | } 34 | return ml; 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /Microsoft/non_overlapping_intervals.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class non_overlapping_intervals { 4 | 5 | 6 | // algorithm: sort on end point 7 | 8 | // why we sort on end point - because we want to remove the interval with the 9 | // highest end point 10 | 11 | public int eraseOverlapIntervals(int[][] intervals) { 12 | 13 | Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1])); 14 | 15 | int start = intervals[0][0]; 16 | int end = intervals[0][1]; 17 | 18 | int count = 0; 19 | 20 | for (int i = 1; i < intervals.length; i++) { 21 | 22 | if (intervals[i][0] < end) { 23 | 24 | count++; 25 | } 26 | 27 | else { 28 | 29 | start = intervals[i][0]; 30 | end = intervals[i][1]; 31 | } 32 | } 33 | 34 | return count; 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /Top75/stacks/stock_span.java: -------------------------------------------------------------------------------- 1 | package Top75.stacks; 2 | 3 | import java.util.Stack; 4 | 5 | // next greater to left with there index manipulation 6 | 7 | public class stock_span { 8 | 9 | // Function to calculate the span of stock’s price for all n days. 10 | public static int[] calculateSpan(int price[], int n) { 11 | 12 | int[] nsl = new int[n]; 13 | Stack st = new Stack<>(); 14 | 15 | st.push(0); 16 | 17 | nsl[0] = 1; 18 | 19 | for (int i = 1; i < n; i++) { 20 | 21 | while (st.size() > 0 && price[st.peek()] <= price[i]) 22 | st.pop(); 23 | 24 | if (st.size() == 0) { 25 | 26 | nsl[i] = 1; 27 | 28 | } else { 29 | 30 | nsl[i] = i - st.peek(); 31 | } 32 | 33 | st.push(i); 34 | } 35 | 36 | return nsl; 37 | 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Top75/LinkedLists/reverse_nodes_in_k_group.java: -------------------------------------------------------------------------------- 1 | package Top75.LinkedLists; 2 | 3 | public class reverse_nodes_in_k_group { 4 | 5 | static class Node{ 6 | Node next; 7 | int data; 8 | 9 | } 10 | public static Node reverse(Node node, int k) { 11 | // Your code here 12 | 13 | // reversing first k nodes 14 | 15 | Node prev = null; 16 | Node temp = null; 17 | 18 | Node cur = node; 19 | int count = 0; 20 | 21 | while (cur != null && count < k) { 22 | 23 | temp = cur.next; 24 | cur.next = prev; 25 | 26 | prev = cur; 27 | cur = temp; 28 | count++; 29 | 30 | } 31 | 32 | // recursive call will handle further 33 | 34 | if (temp != null) { 35 | 36 | node.next = reverse(temp, k); 37 | } 38 | 39 | return prev; 40 | 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Microsoft/InterviewBit/Medium/longest_increasing_subsequence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // DO NOT MODIFY THE ARGUMENTS WITH "final" PREFIX. IT IS READ ONLY 3 | public int lis(final int[] nums) { 4 | 5 | int[][] dp = new int[nums.length][nums.length + 1]; 6 | 7 | for (int[] row : dp) { 8 | Arrays.fill(row, -1); 9 | } 10 | return util(0, -1, nums, dp); 11 | 12 | 13 | } 14 | 15 | 16 | int util(int ind, int prev, int[] nums, int[][] dp) { 17 | 18 | if (ind == nums.length) 19 | return 0; 20 | 21 | if (dp[ind][prev + 1] != -1) 22 | return dp[ind][prev + 1]; 23 | 24 | int notTake = 0 + util(ind + 1, prev, nums, dp); 25 | 26 | int take = -1; 27 | 28 | if (prev == -1 || nums[ind] > nums[prev]) { 29 | 30 | take = 1 + util(ind + 1, ind, nums, dp); 31 | } 32 | 33 | return dp[ind][prev + 1] = Math.max(take, notTake); 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Microsoft/InterviewBit/Easy/merge_two_sorted_ll.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 3 | 4 | if(l1 == null || l2 == null){ 5 | return l1!=null ? l1 : l2; 6 | } 7 | 8 | ListNode head=new ListNode(-1); 9 | 10 | ListNode curr=head; 11 | 12 | while(l1!=null && l2!=null){ 13 | 14 | if(l1.val= prices.length) { 15 | return 0; 16 | } 17 | if (dp[index][buy] != -1) 18 | return dp[index][buy]; 19 | if (buy == 1) { 20 | return dp[index][buy] = Math.max(-prices[index] + solve(index + 1, 0, prices, dp), 21 | 0 + solve(index + 1, 1, prices, dp)); 22 | } 23 | return dp[index][buy] = Math.max(prices[index] + solve(index + 2, 1, prices, dp), 24 | 0 + solve(index + 1, 0, prices, dp)); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Microsoft/merge_intervals.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | 4 | public class merge_intervals { 5 | public int[][] merge(int[][] intervals) { 6 | 7 | // sort on start point - lambda usage 8 | Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); 9 | 10 | ArrayList arr = new ArrayList<>(); 11 | 12 | for (int[] interval : intervals) { 13 | 14 | if (arr.size() == 0) 15 | arr.add(interval); 16 | 17 | else { 18 | 19 | int[] prev = arr.get(arr.size() - 1); 20 | 21 | if (interval[0] <= prev[1]) 22 | prev[1] = Math.max(interval[1], prev[1]); 23 | 24 | else 25 | arr.add(interval); 26 | 27 | } 28 | } 29 | 30 | // converting an ArrayList of arrays of integers (arr) into a regular 2D array 31 | return arr.toArray(new int[arr.size()][]); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Top75/arrays/find_min_in_rotated_sorted_array.java: -------------------------------------------------------------------------------- 1 | package Top75.arrays; 2 | 3 | // find mid 4 | // low to mid - part 1 , mid to high - part 2 5 | 6 | // ans will always be in the unsorted region try going there 7 | 8 | public class find_min_in_rotated_sorted_array { 9 | 10 | public int findMin(int[] nums) { 11 | 12 | int low = 0; 13 | int high = nums.length - 1; 14 | 15 | if (nums[low] <= nums[high]) 16 | return nums[0]; 17 | 18 | while (low < high) { 19 | 20 | int mid = low + (high - low) / 2; 21 | 22 | if (nums[mid] > nums[mid + 1]) 23 | return nums[mid + 1]; 24 | 25 | else if (nums[mid - 1] > nums[mid]) 26 | return nums[mid]; 27 | 28 | else if (nums[low] <= nums[mid]) 29 | low = mid + 1; 30 | 31 | else if (nums[mid] <= nums[high]) 32 | high = mid - 1; 33 | 34 | } 35 | 36 | return -1; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Microsoft/meeting_room_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class meeting_room_1 { 4 | static boolean canAttendMeetings(int[][] intervals) { 5 | 6 | // sort on start point - lambda usage 7 | Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); 8 | 9 | for (int i = 0; i < intervals.length - 1; i++) { 10 | if (intervals[i][1] > intervals[i + 1][0]) 11 | return false; 12 | } 13 | 14 | return true; 15 | 16 | 17 | } 18 | 19 | public static void main(String[] args) { 20 | 21 | // Start times 22 | int s[] = { 7,2}; 23 | 24 | // Finish times 25 | int f[] = { 10,4 }; 26 | 27 | // Defining an arraylist of meet type 28 | 29 | meeting_room_1 obj = new meeting_room_1(); 30 | 31 | int[][] result = new int[s.length][2]; 32 | 33 | for (int i = 0; i < s.length; i++) { 34 | 35 | result[i][0] = s[i]; 36 | result[i][1] = f[i]; 37 | } 38 | 39 | System.out.println(obj.canAttendMeetings(result)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Microsoft/stocks_part5.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class stocks_part5 { 4 | public int maxProfit(int[] prices, int fee) { 5 | 6 | int[][] dp = new int[prices.length][2]; 7 | 8 | for (int[] row : dp) { 9 | 10 | Arrays.fill(row, -1); 11 | } 12 | 13 | return solve2(prices, 0, 1, dp, fee); 14 | } 15 | 16 | int solve2(int[] prices, int i, int buy, int[][] dp, int fee) { 17 | 18 | if (i == prices.length) 19 | return 0; 20 | 21 | if (dp[i][buy] != -1) 22 | return dp[i][buy]; 23 | 24 | int profit = 0; 25 | 26 | if (buy == 1) { 27 | 28 | profit += Math.max((-prices[i] + solve2(prices, i + 1, 0, dp, fee)), 29 | (solve2(prices, i + 1, 1, dp, fee))); 30 | } 31 | 32 | else { 33 | 34 | profit += Math.max((prices[i] - fee + solve2(prices, i + 1, 1, dp, fee)), 35 | (solve2(prices, i + 1, 0, dp, fee))); 36 | } 37 | 38 | return dp[i][buy] = profit; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Microsoft/longest_length_palindromic_String.java: -------------------------------------------------------------------------------- 1 | public class longest_length_palindromic_String { 2 | 3 | public int countSubstrings2(String s) { 4 | 5 | int count = 0; 6 | 7 | boolean[][] dp = new boolean[s.length()][s.length()]; 8 | 9 | int len = 0; 10 | 11 | for (int g = 0; g < s.length(); g++) { 12 | 13 | for (int i = 0, j = g; j < s.length(); i++, j++) { 14 | 15 | if (g == 0) { 16 | dp[i][j] = true; 17 | } else if (g == 1) { 18 | if (s.charAt(i) == s.charAt(j)) { 19 | dp[i][j] = true; 20 | } else { 21 | dp[i][j] = false; 22 | } 23 | } else { 24 | if (s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1]) { 25 | dp[i][j] = true; 26 | } else { 27 | dp[i][j] = false; 28 | } 29 | } 30 | 31 | if (dp[i][j]) { 32 | len = g+1; 33 | } 34 | } 35 | } 36 | 37 | return len; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Microsoft/merge_two_sorted_arrays.java: -------------------------------------------------------------------------------- 1 | public class merge_two_sorted_arrays { 2 | 3 | public double findMedianSortedArrays(int[] nums1, int[] nums2) { 4 | 5 | // time complexity : O(n+m) , space complexity : O(n+m) 6 | // brute force approach 7 | int[] nums3 = new int[nums1.length + nums2.length]; 8 | int i = 0; 9 | int j = 0; 10 | int k = 0; 11 | 12 | while (i < nums1.length && j < nums2.length) { 13 | if (nums1[i] < nums2[j]) { 14 | nums3[k] = nums1[i]; 15 | k++; 16 | i++; 17 | 18 | } else { 19 | nums3[k] = nums2[j]; 20 | k++; 21 | j++; 22 | 23 | } 24 | 25 | } 26 | 27 | while (i < nums1.length) { 28 | nums3[k] = nums1[i]; 29 | k++; 30 | i++; 31 | } 32 | 33 | while (j < nums2.length) { 34 | nums3[k] = nums2[j]; 35 | j++; 36 | k++; 37 | } 38 | 39 | if (nums3.length % 2 != 0) 40 | return (double) nums3[nums3.length / 2]; 41 | 42 | return (double) (nums3[(nums3.length - 1) / 2] + nums3[nums3.length / 2]) / 2.0; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Top75/arrays/search_in_rotated_sorted_array.java: -------------------------------------------------------------------------------- 1 | package Top75. arrays; 2 | 3 | import java.util.Scanner; 4 | 5 | 6 | public class search_in_rotated_sorted_array { 7 | 8 | public int search(int[] arr, int target) { 9 | 10 | int low = 0; 11 | int high = arr.length - 1; 12 | while (low <= high) { 13 | 14 | int mid = (low + high) / 2; 15 | 16 | // this means low to mid part is sorted 17 | 18 | if (arr[mid] == target) 19 | return mid; 20 | if (arr[low] <= arr[mid]) { 21 | 22 | if (target >= arr[low] && arr[mid] > target) 23 | high = mid - 1; 24 | else 25 | low = mid + 1; 26 | } 27 | 28 | else if 29 | 30 | (arr[mid] <= arr[high]) { 31 | 32 | if (target > arr[mid] && target <= arr[high]) { 33 | 34 | low = mid + 1; 35 | } 36 | 37 | else 38 | high = mid - 1; 39 | } 40 | 41 | } 42 | 43 | return -1; 44 | 45 | } 46 | 47 | public static void main(String[] args) { 48 | Scanner s = new Scanner(System.in); 49 | 50 | 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Top75/DP/max_sum_of_non_adjacent.java: -------------------------------------------------------------------------------- 1 | package Top75.DP; 2 | import java.util.Arrays; 3 | 4 | // memoization soln 5 | 6 | class Solution 7 | 8 | { 9 | 10 | public int FindMaxSum(int arr[], int n) 11 | 12 | { 13 | 14 | 15 | int[] dp = new int[n+1]; 16 | 17 | Arrays.fill(dp , -1); 18 | 19 | int ans = solve(arr , n-1 , dp); 20 | 21 | 22 | return ans; 23 | 24 | } 25 | 26 | static int solve(int[] nums , int n , int[] dp){ 27 | 28 | 29 | if(n < 0) return 0; 30 | 31 | if(n == 0) return nums[0]; 32 | 33 | if(dp[n] != -1) return dp[n]; 34 | 35 | int incl = solve(nums , n-2,dp) + nums[n]; 36 | int exl = solve(nums , n-1,dp) + 0; 37 | 38 | return dp[n] = Math.max(incl , exl); 39 | } 40 | 41 | // Function to find the maximum money the thief can get. 42 | public int tabulation(int arr[], int n) { 43 | int dp[] = new int[n + 1]; 44 | dp[0] = 0; 45 | dp[1] = Math.max(dp[0], arr[0]); 46 | for (int i = 2; i <= n; i++) { 47 | dp[i] = Math.max(dp[i - 1], arr[i - 1] + dp[i - 2]); 48 | } 49 | return dp[n]; 50 | } 51 | } -------------------------------------------------------------------------------- /Top75/strings/longest_string_without_repeating_characters.java: -------------------------------------------------------------------------------- 1 | package Top75.strings; 2 | import java.util.*; 3 | class Solution { 4 | public int lengthOfLongestSubstring(String s) { 5 | 6 | HashMap map = new HashMap<>(); 7 | int ans = 0; 8 | 9 | int i = -1, j = -1; 10 | 11 | while (true) { 12 | boolean f1 = false; 13 | boolean f2 = false; 14 | 15 | // Accquire 16 | while (i < s.length() - 1) { 17 | f1 = true; 18 | i++; 19 | char ch = s.charAt(i); 20 | map.put(ch, map.getOrDefault(ch, 0) + 1); 21 | 22 | if (map.get(ch) == 2) 23 | break; 24 | 25 | else { 26 | int len = i - j; 27 | ans = Math.max(ans, len); 28 | } 29 | } 30 | 31 | // Release 32 | while (j < i) { 33 | f2 = true; 34 | j++; 35 | char ch = s.charAt(j); 36 | map.put(ch, map.get(ch) - 1); 37 | 38 | if (map.get(ch) == 1) 39 | break; 40 | 41 | } 42 | 43 | if (f1 == false && f2 == false) 44 | break; 45 | } 46 | 47 | return ans; 48 | 49 | } 50 | } -------------------------------------------------------------------------------- /Microsoft/stocks_part3.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | 4 | // memoized - 3D DP PROBLEM 5 | 6 | // AT MOST 2 TRANSACTIONS 7 | /** 8 | * stocks_part3 9 | */ 10 | public class stocks_part3 { 11 | 12 | static int maxProfit(int K, int N, int A[]) { 13 | // code here 14 | 15 | int[][][] dp = new int[3][N + 1][K + 1]; 16 | 17 | for (int[][] row : dp) { 18 | 19 | for (int[] it : row) { 20 | 21 | Arrays.fill(it, -1); 22 | } 23 | } 24 | 25 | return solve(A, 0, 1, N, K, dp); 26 | } 27 | 28 | static int solve(int[] A, int index, int buy, int n, int k, int[][][] dp) { 29 | 30 | if (k == 0) 31 | return 0; 32 | if (index == A.length) 33 | return 0; 34 | 35 | if (dp[buy][index][k] != -1) 36 | return dp[buy][index][k]; 37 | 38 | int profit = 0; 39 | 40 | if (buy == 1) { 41 | 42 | int buyKaro = -A[index] + solve(A, index + 1, 0, n, k, dp); 43 | int skipKaro = 0 + solve(A, index + 1, 1, n, k, dp); 44 | 45 | profit = Math.max(buyKaro, skipKaro); 46 | } 47 | 48 | else { 49 | 50 | int sellKaro = A[index] + solve(A, index + 1, 1, n, k - 1, dp); 51 | int skipKaro = 0 + solve(A, index + 1, 0, n, k, dp); 52 | 53 | profit = Math.max(sellKaro, skipKaro); 54 | } 55 | 56 | return dp[buy][index][k] = profit; 57 | 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /Microsoft/InterviewBit/Easy/majority_element.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | // brute force O(N) space and O(N) time 4 | 5 | public int majorityElement(final int[] A) { 6 | 7 | int ans = -1; 8 | 9 | HashMap map = new HashMap(); 10 | 11 | for(int a : A ){ 12 | 13 | map.put(a , map.getOrDefault(a , 0 ) + 1); 14 | } 15 | 16 | 17 | 18 | for(int a : map.keySet()){ 19 | 20 | if(map.get(a) > A.length / 2){ 21 | 22 | ans = a; 23 | break; 24 | } 25 | } 26 | 27 | return ans; 28 | } 29 | 30 | public static int majorityElement2(int[] arr){ 31 | 32 | int ansIndex = 0; 33 | int count = 1; 34 | 35 | for(int i = 1; i < arr.length ; i++){ 36 | 37 | if(arr[ansIndex] == arr[i]){ 38 | 39 | count++; 40 | } 41 | else count--; 42 | 43 | if(count == 0){ 44 | 45 | ansIndex = i; 46 | count = 1; 47 | } 48 | } 49 | 50 | int temp = 0; 51 | for(int i = 0 ; i < arr.length ; i++){ 52 | 53 | if(arr[i] == arr[ansIndex]){ 54 | 55 | temp++; 56 | } 57 | } 58 | 59 | if(temp > arr.length / 2) return arr[ansIndex]; 60 | else return -1; 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Microsoft/stocks_part4.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class stocks_part4 { 4 | 5 | // memoized - k transactions ques 6 | 7 | public int maxProfit(int K, int[] A) { 8 | 9 | int N = A.length; 10 | int[][][] dp = new int[3][N + 1][K + 1]; 11 | 12 | for (int[][] row : dp) { 13 | 14 | for (int[] it : row) { 15 | 16 | Arrays.fill(it, -1); 17 | } 18 | } 19 | 20 | return solve(A, 0, 1, N, K, dp); 21 | } 22 | 23 | static int solve(int[] A, int index, int buy, int n, int k, int[][][] dp) { 24 | 25 | if (k == 0) 26 | return 0; 27 | if (index == A.length) 28 | return 0; 29 | 30 | if (dp[buy][index][k] != -1) 31 | return dp[buy][index][k]; 32 | 33 | int profit = 0; 34 | 35 | if (buy == 1) { 36 | 37 | int buyKaro = -A[index] + solve(A, index + 1, 0, n, k, dp); 38 | int skipKaro = 0 + solve(A, index + 1, 1, n, k, dp); 39 | 40 | profit = Math.max(buyKaro, skipKaro); 41 | } 42 | 43 | else { 44 | 45 | int sellKaro = A[index] + solve(A, index + 1, 1, n, k - 1, dp); 46 | int skipKaro = 0 + solve(A, index + 1, 0, n, k, dp); 47 | 48 | profit = Math.max(sellKaro, skipKaro); 49 | } 50 | 51 | return dp[buy][index][k] = profit; 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /Top75/LinkedLists/copy_list_with_random_pointer.java: -------------------------------------------------------------------------------- 1 | package Top75.LinkedLists; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class copy_list_with_random_pointer { 7 | class Node { 8 | int val; 9 | Node next; 10 | Node random; 11 | 12 | public Node(int val) { 13 | this.val = val; 14 | this.next = null; 15 | this.random = null; 16 | } 17 | } 18 | public Node copyRandomList(Node head) { 19 | Map nodes = new HashMap<>(); 20 | Node newHead = null; 21 | Node temp = head; 22 | Node newTemp = null; 23 | while (temp != null) { 24 | if (newHead == null) { 25 | newHead = new Node(temp.val); 26 | newTemp = newHead; 27 | } else { 28 | Node newNode = new Node(temp.val); 29 | newTemp.next = newNode; 30 | newTemp = newTemp.next; 31 | } 32 | nodes.put(temp, newTemp); 33 | temp = temp.next; 34 | } 35 | temp = head; 36 | newTemp = newHead; 37 | while (temp != null) { 38 | if (temp.random == null) { 39 | newTemp.random = null; 40 | } else { 41 | newTemp.random = nodes.get(temp.random); 42 | } 43 | temp = temp.next; 44 | newTemp = newTemp.next; 45 | } 46 | return newHead; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Top75/LinkedLists/merge_k_sorted_LL.java: -------------------------------------------------------------------------------- 1 | public class merge_k_sorted_LL { 2 | 3 | static class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode() { 8 | } 9 | 10 | ListNode(int val) { 11 | this.val = val; 12 | } 13 | 14 | ListNode(int val, ListNode next) { 15 | this.val = val; 16 | this.next = next; 17 | } 18 | } 19 | 20 | public ListNode mergeKLists(ListNode[] lists) { 21 | 22 | if (lists.length == 0) 23 | return null; 24 | return mergeKLists(lists, 0, lists.length - 1); 25 | } 26 | 27 | public ListNode mergeKLists(ListNode[] lists, int si, int ei) { 28 | 29 | if (si > ei) 30 | return null; 31 | 32 | if (si == ei) 33 | return lists[si]; 34 | 35 | int mid = (si + ei) / 2; 36 | 37 | ListNode left = mergeKLists(lists, si, mid); 38 | ListNode right = mergeKLists(lists, mid + 1, ei); 39 | 40 | return mergeTwoLists(left, right); 41 | 42 | } 43 | 44 | public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { 45 | if (l1 == null || l2 == null) 46 | return l1 != null ? l1 : l2; 47 | 48 | ListNode dummy = new ListNode(-1); 49 | ListNode prev = dummy; 50 | 51 | ListNode c1 = l1; 52 | ListNode c2 = l2; 53 | 54 | while (c1 != null && c2 != null) { 55 | if (c1.val <= c2.val) { 56 | prev.next = c1; 57 | c1 = c1.next; 58 | } else { 59 | prev.next = c2; 60 | c2 = c2.next; 61 | } 62 | prev = prev.next; 63 | } 64 | 65 | prev.next = (c1 != null ? c1 : c2); 66 | 67 | return dummy.next; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Top75/stacks/largest_area_histogram.java: -------------------------------------------------------------------------------- 1 | package Top75.stacks; 2 | import java.util.Stack; 3 | 4 | class Solution { 5 | public int largestRectangleArea(int[] heights) { 6 | int max = 0; 7 | int[] nsr = nextSmallerRight(heights); 8 | int[] nsl = nextSmallerleft(heights); 9 | for (int i = 0; i < heights.length; i++) { 10 | int area = heights[i] * (nsr[i] - nsl[i] - 1); 11 | if (area > max) 12 | max = area; 13 | } 14 | 15 | return max; 16 | 17 | } 18 | 19 | public static int[] nextSmallerRight(int[] heights) { 20 | Stack st = new Stack<>(); 21 | int[] nsr = new int[heights.length]; 22 | st.push(heights.length - 1); 23 | nsr[heights.length - 1] = heights.length; 24 | 25 | for (int i = heights.length - 2; i >= 0; i--) { 26 | 27 | while (st.size() > 0 && heights[st.peek()] >= heights[i]) { 28 | st.pop(); 29 | } 30 | 31 | if (st.isEmpty()) { 32 | nsr[i] = heights.length; 33 | } else { 34 | nsr[i] = st.peek(); 35 | } 36 | st.push(i); 37 | } 38 | 39 | return nsr; 40 | } 41 | 42 | public static int[] nextSmallerleft(int[] heights) { 43 | Stack st = new Stack<>(); 44 | int[] nsl = new int[heights.length]; 45 | st.push(0); 46 | nsl[0] = -1; 47 | for (int i = 1; i < heights.length; i++) { 48 | 49 | while (st.size() > 0 && heights[st.peek()] >= heights[i]) { 50 | st.pop(); 51 | } 52 | 53 | if (st.isEmpty()) { 54 | nsl[i] = -1; 55 | } else { 56 | nsl[i] = st.peek(); 57 | } 58 | 59 | st.push(i); 60 | } 61 | 62 | return nsl; 63 | } 64 | } -------------------------------------------------------------------------------- /Microsoft/count_palindromic_substrings.java: -------------------------------------------------------------------------------- 1 | public class count_palindromic_substrings { 2 | 3 | // Count Palindromic Substrings brute force 4 | // O(n^3) time 5 | public int countSubstrings(String s) { 6 | 7 | int count = 0; 8 | 9 | for (int i = 0; i < s.length(); i++) { 10 | 11 | for (int j = i + 1; j <= s.length(); j++) { 12 | 13 | if (isPalindrome(s.substring(i, j))) { 14 | count++; 15 | } else { 16 | continue; 17 | } 18 | } 19 | } 20 | 21 | return count; 22 | } 23 | 24 | boolean isPalindrome(String s) { 25 | 26 | int i = 0; 27 | int j = s.length() - 1; 28 | 29 | while (i <= j) { 30 | 31 | if (s.charAt(i) == s.charAt(j)) { 32 | i++; 33 | j--; 34 | } else { 35 | return false; 36 | } 37 | } 38 | 39 | return true; 40 | } 41 | 42 | // optimized O(n^2) time , gap method, tabulation 43 | 44 | public int countSubstrings2(String s) { 45 | 46 | int count = 0; 47 | 48 | boolean[][] dp = new boolean[s.length()][s.length()]; 49 | 50 | for (int g = 0; g < s.length(); g++) { 51 | 52 | for (int i = 0, j = g; j < s.length(); i++, j++) { 53 | 54 | if (g == 0) { 55 | dp[i][j] = true; 56 | } else if (g == 1) { 57 | if (s.charAt(i) == s.charAt(j)) { 58 | dp[i][j] = true; 59 | } else { 60 | dp[i][j] = false; 61 | } 62 | } else { 63 | if (s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1]) { 64 | dp[i][j] = true; 65 | } else { 66 | dp[i][j] = false; 67 | } 68 | } 69 | 70 | if (dp[i][j]) { 71 | count++; 72 | } 73 | } 74 | } 75 | 76 | return count; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Top75/arrays/ThreeSum.java: -------------------------------------------------------------------------------- 1 | package Top75.arrays; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | 8 | // brute force solution 9 | public class ThreeSum { 10 | class Solution { 11 | public List> threeSum(int[] nums) { 12 | 13 | Arrays.sort(nums); 14 | List> ans = new ArrayList<>(); 15 | for (int i = 0; i < nums.length; i++) { 16 | 17 | for (int j = i + 1; j < nums.length; j++) { 18 | for (int k = j + 1; k < nums.length; k++) { 19 | if (nums[i] + nums[j] + nums[k] == 0) { 20 | ans.add(Arrays.asList(nums[i], nums[j], nums[k])); 21 | } 22 | } 23 | } 24 | } 25 | 26 | HashSet> set = new HashSet(ans); 27 | List> res = new ArrayList<>(set); 28 | 29 | return res; 30 | } 31 | } 32 | 33 | } 34 | class Solution { 35 | public List> threeSum(int[] nums) { 36 | 37 | List> res = new ArrayList<>(); 38 | int n = nums.length; 39 | 40 | if(n < 3){ 41 | return res; 42 | } 43 | 44 | Arrays.sort(nums); 45 | 46 | for(int i = 0 ; i <= n - 3 ; i++){ 47 | 48 | if(i !=0 && nums[i] == nums[i-1]) continue; 49 | 50 | int val1 = nums[i]; 51 | int targ = 0 - val1; 52 | List> subres = twoSum(nums , i+1, n -1 , targ); 53 | 54 | // impact of val1 in these pairs 55 | 56 | for(List list : subres){ 57 | list.add(val1); 58 | res.add(list); 59 | 60 | } 61 | } 62 | return res; 63 | } 64 | 65 | public List> twoSum(int[] nums , int s1 , int e1 , int target){ 66 | 67 | int left = s1; 68 | int right = e1; 69 | List> res = new ArrayList<>(); 70 | int n = nums.length; 71 | 72 | while(left subres = new ArrayList<>(); 80 | subres.add(nums[left]); 81 | subres.add(nums[right]); 82 | res.add(subres); 83 | left++; right--; 84 | 85 | } 86 | 87 | else if(sum > target) right--; 88 | 89 | else left++; 90 | 91 | } 92 | 93 | return res; 94 | 95 | 96 | } 97 | } -------------------------------------------------------------------------------- /Microsoft/stocks_part2.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class stocks_part2 { 4 | 5 | 6 | // recursive solution 7 | // time complexity : O(2^n) , space complexity : O(n) 8 | public int maxProfit(int[] prices) { 9 | 10 | return solve(prices, 0, 1); 11 | } 12 | 13 | static int solve(int[] prices, int i, int buy) { 14 | 15 | if (i == prices.length) 16 | return 0; 17 | 18 | int profit = 0; 19 | 20 | if (buy == 1) { 21 | 22 | profit += Math.max((-prices[i] + solve(prices, i + 1, 0)), (solve(prices, i + 1, 1))); 23 | } 24 | 25 | else { 26 | 27 | profit += Math.max((prices[i] + solve(prices, i + 1, 1)), (solve(prices, i + 1, 0))); 28 | } 29 | 30 | return profit; 31 | 32 | } 33 | 34 | // memoization 35 | 36 | class stocks_part2_1 { 37 | public int maxProfit2(int[] prices) { 38 | 39 | int[][] dp = new int[prices.length][2]; 40 | 41 | for (int[] row : dp) { 42 | 43 | Arrays.fill(row, -1); 44 | } 45 | 46 | return solve2(prices, 0, 1, dp); 47 | } 48 | 49 | int solve2(int[] prices, int i, int buy, int[][] dp) { 50 | 51 | if (i == prices.length) 52 | return 0; 53 | 54 | if (dp[i][buy] != -1) 55 | return dp[i][buy]; 56 | 57 | int profit = 0; 58 | 59 | if (buy == 1) { 60 | 61 | profit += Math.max((-prices[i] + solve2(prices, i + 1, 0, dp)), (solve2(prices, i + 1, 1, dp))); 62 | } 63 | 64 | else { 65 | 66 | profit += Math.max((prices[i] + solve2(prices, i + 1, 1, dp)), (solve2(prices, i + 1, 0, dp))); 67 | } 68 | 69 | return dp[i][buy] = profit; 70 | 71 | } 72 | } 73 | 74 | class stocks_part2_2{ 75 | 76 | public int maxProfit3(int[] prices) { 77 | 78 | int[][] dp = new int[prices.length][2]; 79 | 80 | for (int[] row : dp) { 81 | 82 | Arrays.fill(row, -1); 83 | } 84 | 85 | return solve3(prices, dp); 86 | } 87 | 88 | int solve3(int[] prices, int[][] dp) { 89 | 90 | 91 | for (int i = prices.length - 1; i >= 0; i--) { 92 | 93 | for (int buy = 0; buy < 2; buy++) { 94 | 95 | if (i == prices.length - 1) { 96 | 97 | dp[i][buy] = 0; 98 | continue; 99 | } 100 | 101 | int profit = 0; 102 | 103 | if (buy == 1) { 104 | 105 | profit += Math.max((-prices[i] + dp[i + 1][0]), (dp[i + 1][1])); 106 | } 107 | 108 | else { 109 | 110 | profit += Math.max((prices[i] + dp[i + 1][1]), (dp[i + 1][0])); 111 | } 112 | 113 | dp[i][buy] = profit; 114 | } 115 | } 116 | 117 | return dp[0][1]; 118 | 119 | } 120 | 121 | public class stocks_part2_3 { 122 | 123 | // space optimized solution 124 | 125 | public int maxProfit4(int[] prices) { 126 | 127 | int[][] cur = new int[2][2]; 128 | int[][] prev = new int[2][2]; 129 | 130 | for (int[] row : cur) { 131 | 132 | Arrays.fill(row, -1); 133 | } 134 | 135 | for (int[] row : prev) { 136 | 137 | Arrays.fill(row, -1); 138 | } 139 | 140 | for (int i = prices.length - 1; i >= 0; i--) { 141 | 142 | for (int buy = 0; buy < 2; buy++) { 143 | 144 | if (i == prices.length - 1) { 145 | 146 | cur[buy][0] = 0; 147 | continue; 148 | } 149 | 150 | int profit = 0; 151 | 152 | if (buy == 1) { 153 | 154 | profit += Math.max((-prices[i] + prev[0][0]), (prev[1][0])); 155 | } 156 | 157 | else { 158 | 159 | profit += Math.max((prices[i] + prev[1][0]), (prev[0][0])); 160 | } 161 | 162 | cur[buy][0] = profit; 163 | } 164 | 165 | prev = cur; 166 | cur = new int[2][2]; 167 | } 168 | 169 | return prev[1][0]; 170 | 171 | 172 | } 173 | 174 | 175 | } 176 | } 177 | 178 | 179 | 180 | 181 | 182 | // tabulation 183 | 184 | 185 | 186 | 187 | 188 | } 189 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Operating System InterviewQuestions 2 | 3 | ## Microsoft SDE sheet : 4 | 5 | https://docs.google.com/document/d/1uY8U4JFndDC0gDxqrGxQSL5a8EViVy5eKBA8jTQYyNs/edit?usp=sharing 6 | ## Gfg Last Minutes Notes: 7 | 8 | https://www.geeksforgeeks.org/last-minute-notes-operating-systems/ 9 | 10 | ## Basic OS Interview Questions 11 | 12 | ### 1.    Why is the operating system important? 13 | 14 | ### 2.    What's the main purpose of an OS? What are the different types of OS? 15 | 16 | ### 3.    What are the benefits of a multiprocessor system? 17 | 18 | ### 4.    What is RAID structure in OS? What are the different levels of RAID configuration? 19 | 20 | ### 5.    What is GUI? 21 | 22 | ### 6.    What is a Pipe and when it is used? 23 | 24 | ### 7.    What are the different kinds of operations that are possible on semaphore? 25 | 26 | ### 8.    What is a bootstrap program in OS? 27 | 28 | ### 9.    Explain demand paging? 29 | 30 | ### 10.    What do you mean by RTOS? 31 | 32 | ### 11.    What do you mean by process synchronization? 33 | 34 | ### 12.    What is IPC? What are the different IPC mechanisms? 35 | 36 | ### 13.    What is different between main memory and secondary memory. 37 | 38 | ### 14.    What do you mean by overlays in OS? 39 | 40 | ### 15.    Write top 10 examples of OS? 41 | 42 | ## Intermediate OS Interview Questions 43 | 44 | ### 16.    What is virtual memory? 45 | 46 | ### 17.    What is thread in OS? 47 | 48 | ### 18.    What is a process? What are the different states of a process? 49 | 50 | # Contents 51 | 52 | ### Intermediate OS Interview Questions (.....Continued) 53 | 54 | ### 19.    What do you mean by FCFS? 55 | 56 | ### 20.    What is Reentrancy? 57 | 58 | ### 21.    What is a Scheduling Algorithm? Name different types of scheduling algorithms. 59 | 60 | ### 22.    What is the difference between paging and segmentation? 61 | 62 | ### 23.    What is thrashing in OS? 63 | 64 | ### 24.    What is the main objective of multiprogramming? 65 | 66 | ### 25.    What do you mean by asymmetric clustering? 67 | 68 | ### 26.    What is the difference between multitasking and multiprocessing OS? 69 | 70 | ### 27.    What do you mean by Sockets in OS? 71 | 72 | ### 28.    Explain zombie process? 73 | 74 | ### 29.    What do you mean by cascading termination? 75 | 76 | ### 30.    What is starvation and aging in OS? 77 | 78 | ### Advanced OS Interview Questions 79 | 80 | ### 31.    What do you mean by Semaphore in OS? Why is it used? 81 | 82 | ### 32.    What is Kernel and write its main functions? 83 | 84 | ### 33.    What are different types of Kernel? 85 | 86 | ### 34.    Write difference between micro kernel and monolithic kernel? 87 | 88 | ### 35.    What is SMP (Symmetric Multiprocessing)? 89 | 90 | ### 36.    What is a time-sharing system? 91 | 92 | ### Advanced OS Interview Questions (.....Continued) 93 | 94 | ### 37.    What is Context Switching? 95 | 96 | ### 38.    What is difference between Kernel and OS? 97 | 98 | ### 39.    What is difference between process and thread? 99 | 100 | ### 40.    What are various sections of the process? 101 | 102 | ### 41.    What is a deadlock in OS? What are the necessary conditions for a deadlock? 103 | 104 | ### 42.    What do you mean by Belady’s Anomaly? 105 | 106 | ### 43.    What is spooling in OS? 107 | 108 | ##### What do you mean by an operating system? What are its basic 109 | 110 | ##### functions? 111 | 112 | Operating System (OS) is basically a soware program that manages and handles all 113 | resources of a computer such as hardware and soware. The first OS was introduced 114 | in the early 1950s known as GMOs. An OS is responsible for managing, handling, and 115 | coordinating overall activities and sharing of computer resources. It acts as an 116 | intermediary among users of computer and computer hardware. 117 | 118 | **Functions of OS:** 119 | There are many functions of the OS. Some of the important functions of OS are given 120 | below: 121 | 122 | # Let's get Started 123 | 124 | ``` 125 | Memory and Processor Management 126 | Providing user interface to users 127 | File Management and Device Management 128 | Scheduling of resources and jobs 129 | Error Detection 130 | Security 131 | ``` 132 | 133 | ### Basic OS Interview Questions 134 | 135 | #### 1.   Why is the operating system important? 136 | 137 | OS is the most essential and vital part of a computer without which it is considered 138 | useless. It enables an interface or acts like a link for interaction between computer 139 | soware that is installed on OS and users. It also helps to communicate with 140 | hardware and also maintains balance among hardware and CPU. It also provides 141 | services to users and a platform for programs to run on. It performs all common tasks 142 | applications require. 143 | 144 | #### 2.   What's the main purpose of an OS? What are the different 145 | 146 | #### types of OS? 147 | 148 | The main purpose of an OS is to execute user programs and make it easier for users 149 | to understand and interact with computers as well as run applications. It is specially 150 | designed to ensure that the computer system performs better by managing all 151 | computational activities. It also manages computer memory, processes, and 152 | operation of all hardware and soware. 153 | 154 | **Types of OS:** 155 | 156 | ``` 157 | Batched OS (Example: Payroll System, Transactions Process, etc.) 158 | Multi-Programmed OS (Example: Windows O/S, UNIX O/S, etc.) 159 | Timesharing OS (Example: Multics, etc.) 160 | Distributed OS (LOCUS, etc.) 161 | Real-Time OS (PSOS, VRTX, etc.) 162 | ``` 163 | 164 | #### 3.   What are the benefits of a multiprocessor system? 165 | 166 | A Multiprocessor system is a type of system that includes two or more CPUs. It 167 | involves the processing of different computer programs at the same time mostly by a 168 | computer system with two or more CPUs that are sharing single memory. 169 | 170 | **Benefits:** 171 | 172 | ``` 173 | Such systems are used widely nowadays to improve performance in systems 174 | that are running multiple programs concurrently.  175 | By increasing the number of processors, a greater number of tasks can be 176 | completed in unit time.  177 | One also gets a considerable increase in throughput and is cost-effective also as 178 | all processors share the same resources. 179 | It simply improves the reliability of the computer system. 180 | ``` 181 | 182 | #### 4.   What is RAID structure in OS? What are the different levels of 183 | 184 | #### RAID configuration? 185 | 186 | RAID (Redundant Arrays of Independent Disks) is a method used to store data on 187 | Multiple hard disks therefore it is considered as data storage virtualization 188 | technology that combines multiple hard disks. It simply balances data protection, 189 | system performance, storage space, etc. It is used to improve the overall 190 | performance and reliability of data storage. It also increases the storage capacity of 191 | the system and its main purpose is to achieve data redundancy to reduce data loss. 192 | 193 | **Different levels of RAID** 194 | 195 | Nowadays, RAID is available in various schemes or RAID level as given below: 196 | 197 | ``` 198 | RAID 0 - Non-redundant striping: This level is used to increase the performance 199 | of the server. 200 | RAID 1 - Mirroring and duplexing: This level is also known as disk mirroring and is 201 | considered the simplest way to implement fault tolerance. 202 | RAID 2 - Memory-style error-correcting codes: This level generally uses dedicated 203 | hamming code parity I.e., a liner form of error correction code. 204 | RAID 3 - Bit-interleaved Parity: This level requires a dedicated parity drive to 205 | store parity information. 206 | RAID 4 - Block-interleaved Parity: This level is similar to RAID 5 but the only 207 | difference is that this level confines all parity data to a single drive. 208 | RAID 5 - Block-interleaved distributed Parity: This level provides far better 209 | performance than disk mirroring and fault tolerance. 210 | RAID 6 - P+Q Redundancy: This level generally provides fault tolerance for two 211 | drive failures. 212 | ``` 213 | 214 | #### 5.   What is GUI? 215 | 216 | GUI (Graphical User Interface) is basically a type of user interface that allows users to 217 | use graphics to interact with OS. GUI is created because it is more user-friendly, less 218 | complex, and easier to understand rather than a command-line interface. Its main 219 | goal is to increase efficiency and ease of use. Instead of having to memorize 220 | commands, users can just click on a button to simply execute the procedure. 221 | Examples of GUI include Microso Windows, macOS, Apple’s iOS, etc. 222 | 223 | #### 6.   What is a Pipe and when it is used? 224 | 225 | The pipe is generally a connection among two or more processes that are 226 | interrelated to each other. It is a mechanism that is used for inter-process 227 | communication using message passing.  One can easily send information such as the 228 | output of one program process to another program process using a pipe. It can be 229 | used when two processes want to communicate one-way i.e., inter-process 230 | communication (IPC). 231 | 232 | #### 7.   What are the different kinds of operations that are possible 233 | 234 | #### on semaphore? 235 | 236 | There are basically two atomic operations that are possible: 237 | 238 | ``` 239 | Wait() 240 | Signal() 241 | ``` 242 | 243 | #### 8.   What is a bootstrap program in OS? 244 | 245 | It is generally a program that initializes OS during startup i.e., first code that is 246 | executed whenever computer system startups. OS is loaded through a bootstrapping 247 | process or program commonly known as booting. Overall OS only depends on the 248 | bootstrap program to perform and work correctly. It is fully stored in boot blocks at a 249 | fixed location on the disk. It also locates the kernel and loads it into the main 250 | memory aer which the program starts its execution. 251 | 252 | #### 9.   Explain demand paging? 253 | 254 | Demand paging is a method that loads pages into memory on demand. This method 255 | is mostly used in virtual memory. In this, a page is only brought into memory when a 256 | location on that particular page is referenced during execution. The following steps 257 | are generally followed: 258 | 259 | ``` 260 | Attempt to access the page. 261 | If the page is valid (in memory) then continue processing instructions as normal. 262 | If a page is invalid then a page-fault trap occurs. 263 | Check if the memory reference is a valid reference to a location on secondary 264 | memory. If not, the process is terminated ( illegal memory access ). Otherwise, 265 | we have to page in the required page. 266 | Schedule disk operation to read the desired page into main memory. 267 | Restart the instruction that was interrupted by the operating system trap. 268 | ``` 269 | 270 | #### 10.   What do you mean by RTOS? 271 | 272 | Real Time Operating System (RTOS) is an operating system that is used for real-time 273 | applications i.e., for those applications where data processing should be done in a 274 | fixed and small measure of time. It performs much better on tasks that are needed to 275 | be executed within a short time. It also takes care of execution, monitoring, and all- 276 | controlling processes. It also occupies less memory and consumes fewer resources. 277 | 278 | **Types of RTOS:** 279 | 280 | ``` 281 | Hard Real-Time 282 | Firm Real-Time 283 | So Real-Time 284 | ``` 285 | 286 | RTOS is used in Air traffic control systems, Anti-lock Brake Systems, and Heart 287 | pacemakers. 288 | 289 | #### 11.   What do you mean by process synchronization? 290 | 291 | Process synchronization is basically a way to coordinate processes that use shared 292 | resources or data. It is very much essential to ensure synchronized execution of 293 | cooperating processes so that will maintain data consistency. Its main purpose is to 294 | share resources without any interference using mutual exclusion. There are two 295 | types of process synchronization: 296 | 297 | ``` 298 | Independent Process 299 | Cooperative Process 300 | ``` 301 | 302 | #### 12.   What is IPC? What are the different IPC mechanisms? 303 | 304 | IPC (Interprocess Communication) is a mechanism that requires the use of resources 305 | like a memory that is shared between processes or threads. With IPC, OS allows 306 | different processes to communicate with each other. It is simply used for exchanging 307 | data between multiple threads in one or more programs or processes. In this 308 | mechanism, different processes can communicate with each other with the approval 309 | of the OS. 310 | 311 | **Different IPC Mechanisms:** 312 | 313 | ``` 314 | Pipes 315 | Message Queuing 316 | Semaphores 317 | Socket 318 | Shared Memory 319 | Signals 320 | ``` 321 | 322 | #### 13.   What is different between main memory and secondary 323 | 324 | #### memory. 325 | 326 | **Main memory:** Main memory in a computer is RAM (Random Access Memory). It is 327 | also known as primary memory or read-write memory or internal memory. The 328 | programs and data that the CPU requires during the execution of a program are 329 | stored in this memory. 330 | **Secondary memory:** Secondary memory in a computer are storage devices that can 331 | store data and programs. It is also known as external memory or additional memory 332 | or backup memory or auxiliary memory. Such storage devices are capable of storing 333 | high-volume data. Storage devices can be hard drives, USB flash drives, CDs, etc. 334 | 335 | ``` 336 | Primary Memory Secondary Memory 337 | ``` 338 | 339 | ``` 340 | Data can be directly 341 | accessed by the processing 342 | unit. 343 | ``` 344 | 345 | ``` 346 | Firstly, data is transferred to 347 | primary memory and aer 348 | then routed to the processing 349 | unit. 350 | ``` 351 | 352 | ``` 353 | It can be both volatile and 354 | non-volatile in nature. 355 | ``` 356 | 357 | ``` 358 | It is non-volatile in nature. 359 | ``` 360 | 361 | ``` 362 | It is more costly than 363 | secondary memory. 364 | ``` 365 | 366 | ``` 367 | It is more cost-effective or less 368 | costly than primary memory. 369 | ``` 370 | 371 | ``` 372 | It is temporary because 373 | data is stored temporarily. 374 | ``` 375 | 376 | ``` 377 | It is permanent because data 378 | is stored permanently. 379 | ``` 380 | 381 | ``` 382 | In this memory, data can be 383 | lost whenever there is a 384 | power failure. 385 | ``` 386 | 387 | ``` 388 | In this memory, data is stored 389 | permanently and therefore 390 | cannot be lost even in case of 391 | power failure. 392 | ``` 393 | 394 | ``` 395 | It is much faster than 396 | secondary memory and 397 | saves data that is currently 398 | used by the computer. 399 | ``` 400 | 401 | ``` 402 | It is slower as compared to 403 | primary memory and saves 404 | different kinds of data in 405 | different formats. 406 | ``` 407 | 408 | ``` 409 | It can be accessed by data. It can be accessed by I/O 410 | channels. 411 | ``` 412 | 413 | #### 14.   What do you mean by overlays in OS? 414 | 415 | Overlays is basically a programming method that divides processes into pieces so 416 | that instructions that are important and need can be saved in memory. It does not 417 | need any type of support from the OS. It can run programs that are bigger in size than 418 | physical memory by only keeping only important data and instructions that can be 419 | needed at any given time. 420 | 421 | #### 15.   Write top 10 examples of OS? 422 | 423 | Some of the top OS’s that are used mostly are given below: 424 | 425 | ``` 426 | MS-Windows 427 | Ubuntu 428 | Mac OS 429 | Fedora 430 | Solaris 431 | Free BSD 432 | Chrome OS 433 | CentOS 434 | Debian 435 | Android 436 | ``` 437 | 438 | ### Intermediate OS Interview Questions 439 | 440 | #### 16.   What is virtual memory? 441 | 442 | It is a memory management technique feature of OS that creates the illusion to users 443 | of a very large (main) memory. It is simply space where a greater number of programs 444 | can be stored by themselves in the form of pages. It enables us to increase the use of 445 | physical memory by using a disk and also allows us to have memory protection. It can 446 | be managed in two common ways by OS i.e., paging and segmentation. It acts as 447 | temporary storage that can be used along with RAM for computer processes. 448 | 449 | #### 17.   What is thread in OS? 450 | 451 | Thread is a path of execution that is composed of a program counter, thread id, 452 | stack, and set of registers within the process. It is a basic unit of CPU utilization that 453 | makes communication more effective and efficient, enables utilization of 454 | multiprocessor architectures to a greater scale and greater efficiency, and reduces 455 | the time required in context switching. It simply provides a way to improve and 456 | increase the performance of applications through parallelism. Threads are 457 | sometimes called **lightweight processes** because they have their own stack but can 458 | access shared data. 459 | 460 | Multiple threads running in a process share: Address space, Heap, Static data, Code 461 | segments, File descriptors, Global variables, Child processes, Pending alarms, Signals, 462 | and signal handlers. 463 | 464 | Each thread has its own: Program counter, Registers, Stack, and State. 465 | 466 | #### 18.   What is a process? What are the different states of a 467 | 468 | #### process? 469 | 470 | The process is basically a program that is currently under execution. The main 471 | function of an OS is to manage and handle all of these processes. When a program is 472 | loaded into the memory and it becomes a process, it can be divided into four 473 | sections ─ stack, heap, text, and data. There are two types of processes: 474 | 475 | ``` 476 | 1. Operating System Processes 477 | 2. User Processes 478 | ``` 479 | 480 | **States of Process:** 481 | 482 | Different states of the process through which process goes are given below: 483 | 484 | ``` 485 | New State : In this state, a process is just created. 486 | Running: In this state, the CPU starts working on the process’s instructions. 487 | Waiting: In this state, the process cannot run because it just waits for some 488 | event to occur 489 | Ready: In this state, the process has all resources available that are required to 490 | run but it waits to get assigned to a processor because CPUs are not working 491 | currently on instructions passed by the process. 492 | Terminate: In this state, the process is completed I.e., the process has finished 493 | execution. 494 | ``` 495 | 496 | #### 19.   What do you mean by FCFS? 497 | 498 | **FCFS** (First Come First Serve) is a type of OS scheduling algorithm that executes 499 | processes in the same order in which processes arrive. In simple words, the process 500 | that arrives first will be executed first. It is non-preemptive in nature. FCFS scheduling 501 | may cause the problem of starvation if the burst time of the first process is the 502 | longest among all the jobs. Burst time here means the time that is required in 503 | milliseconds by the process for its execution. It is also considered the easiest and 504 | simplest OS scheduling algorithm as compared to others. Implementation of FCFS is 505 | generally managed with help of the FIFO (First In First Out) queue. 506 | 507 | #### 20.   What is Reentrancy? 508 | 509 | Reentrant is simply a function in which various clients can use and shares a single 510 | copy of a program during a similar period. This concept is generally associated with 511 | OS code and does not deal with concurrency. It has two major functions: 512 | 513 | ``` 514 | Program code cannot change or modify itself. 515 | Local data for every client process needs to be stored in different disks. 516 | ``` 517 | 518 | #### 21.   What is a Scheduling Algorithm? Name different types of 519 | 520 | #### scheduling algorithms. 521 | 522 | A scheduling algorithm is a process that is used to improve efficiency by utilizing 523 | maximum CPU and providing minimum waiting time to tasks. It simply deals with the 524 | problem of deciding which of outstanding requests is to be allocated resources. Its 525 | main aim is to reduce resource starvation and to ensure fairness amongst parties 526 | that are utilizing the resources. In simple words, it is used to allocate resources 527 | among various competing tasks. 528 | 529 | **Types of Scheduling Algorithm** 530 | 531 | There are different types of scheduling algorithms as given below: 532 | 533 | #### 22.   What is the difference between paging and segmentation? 534 | 535 | **Paging:** It is generally a memory management technique that allows OS to retrieve 536 | processes from secondary storage into main memory. It is a non-contiguous 537 | allocation technique that divides each process in the form of pages.  538 | **Segmentation:** It is generally a memory management technique that divides 539 | processes into modules and parts of different sizes. These parts and modules are 540 | known as segments that can be allocated to process. 541 | 542 | **Paging Segmentation** 543 | 544 | It is invisible to a 545 | programmer. 546 | 547 | ``` 548 | It is visible to a programmer. 549 | ``` 550 | 551 | In this, the size of pages 552 | is fixed. 553 | 554 | ``` 555 | In this, the size of segments is not 556 | fixed. 557 | ``` 558 | 559 | Procedures and data 560 | cannot be separated in 561 | paging. 562 | 563 | ``` 564 | Procedures and data can be 565 | separated in segmentation. 566 | ``` 567 | 568 | It allows a cumulative 569 | total of virtual address 570 | spaces to cross physical 571 | main memory. 572 | 573 | ``` 574 | It allows all programs, data, and 575 | codes to break up into 576 | independent address spaces. 577 | ``` 578 | 579 | It is mostly available on 580 | CPUs and MMU chips. 581 | 582 | ``` 583 | It is mostly available on Windows 584 | servers that may support 585 | backward compatibility, while 586 | Linux has limited support. 587 | ``` 588 | 589 | It is faster for memory 590 | access as compared to 591 | segmentation. 592 | 593 | ``` 594 | It is slower as compared to paging. 595 | ``` 596 | 597 | In this, OS needs to 598 | maintain a free frame. 599 | 600 | ``` 601 | In this, OS needs to maintain a list 602 | of holes in the main memory. 603 | ``` 604 | 605 | In paging, the type of 606 | fragmentation is 607 | internal. 608 | 609 | ``` 610 | In segmentation, the type of 611 | fragmentation is external. 612 | ``` 613 | 614 | The size of the page is 615 | determined by 616 | available memory. 617 | 618 | ``` 619 | The size of the page is determined 620 | by the user. 621 | ``` 622 | 623 | #### 23.   What is thrashing in OS? 624 | 625 | It is generally a situation where the CPU performs less productive work and more 626 | swapping or paging work. It spends more time swapping or paging activities rather 627 | than its execution. By evaluating the level of CPU utilization, a system can detect 628 | thrashing. It occurs when the process does not have enough pages due to which the 629 | page-fault rate is increased. It inhibits much application-level processing that causes 630 | computer performance to degrade or collapse. 631 | 632 | #### 24.   What is the main objective of multiprogramming? 633 | 634 | It refers to the ability to execute or perform more than one program on a single 635 | processor machine. This technique was introduced to overcome the problem of 636 | underutilization of CPU and main memory. In simple words, it is the coordination of 637 | execution of various programs simultaneously on a single processor (CPU). The main 638 | objective of multiprogramming is to have at least some processes running at all 639 | times. It simply improves the utilization of the CPU as it organizes many jobs where 640 | the CPU always has one to execute. 641 | 642 | #### 25.   What do you mean by asymmetric clustering? 643 | 644 | Asymmetric Clustering is generally a system in which one of the nodes among all 645 | nodes is in hot standby mode whereas the rest of all nodes run different applications. 646 | It simply uses whole or entire hardware resources therefore it is considered a more 647 | reliable system as compared to others. 648 | 649 | #### 26.   What is the difference between multitasking and 650 | 651 | #### multiprocessing OS? 652 | 653 | **Multitasking:** It is a system that allows more efficient use of computer hardware. 654 | This system works on more than one task at one time by rapidly switching between 655 | various tasks. These systems are also known as time-sharing systems. 656 | 657 | **Multiprocessing:** It is a system that allows multiple or various processors in a 658 | computer to process two or more different portions of the same program 659 | simultaneously. It is used to complete more work in a shorter period of time. 660 | 661 | ``` 662 | Multitasking Multiprocessing 663 | ``` 664 | 665 | ``` 666 | It performs more than one 667 | task at a time using a single 668 | processor. 669 | ``` 670 | 671 | ``` 672 | It performs more than one 673 | task at a time using multiple 674 | processors. 675 | ``` 676 | 677 | ``` 678 | In this, the number of CPUs is 679 | only one. 680 | ``` 681 | 682 | ``` 683 | In this, the number of CPUs 684 | is more than one. 685 | ``` 686 | 687 | ``` 688 | It is more economical. It is less economical. 689 | ``` 690 | 691 | ``` 692 | It is less efficient than 693 | multiprocessing. 694 | ``` 695 | 696 | ``` 697 | It is more efficient than 698 | multitasking. 699 | ``` 700 | 701 | ``` 702 | It allows fast switching among 703 | various tasks. 704 | ``` 705 | 706 | ``` 707 | It allows smooth processing 708 | of multiple tasks at once. 709 | ``` 710 | 711 | ``` 712 | It requires more time to 713 | execute tasks as compared to 714 | multiprocessing. 715 | ``` 716 | 717 | ``` 718 | It requires less time for job 719 | processing as compared to 720 | multitasking. 721 | ``` 722 | 723 | #### 27.   What do you mean by Sockets in OS? 724 | 725 | The socket in OS is generally referred to as an endpoint for IPC (Interprocess 726 | Communication). Here, the endpoint is referred to as a combination of an IP address 727 | and port number.  Sockets are used to make it easy for soware developers to create 728 | network-enabled programs. It also allows communication or exchange of 729 | information between two different processes on the same or different machines. It is 730 | mostly used in client-server-based systems. 731 | 732 | **Types of Sockets** 733 | 734 | There are basically four types of sockets as given below: 735 | 736 | ``` 737 | Stream Sockets 738 | Datagram Sockets 739 | Sequenced Packet Sockets 740 | Raw Sockets 741 | ``` 742 | 743 | #### 28.   Explain zombie process? 744 | 745 | Zombie process, referred to as a defunct process, is basically a process that is 746 | terminated or completed but the whole process control block is not cleaned up from 747 | the main memory because it still has an entry in the process table to report to its 748 | parent process. It does not consume any of the resources and is dead, but it still 749 | exists. It also shows that resources are held by process and are not free. 750 | 751 | #### 29.   What do you mean by cascading termination? 752 | 753 | Cascading termination is a process termination in which if the parent process is 754 | exiting or terminating then the children process will also get terminated. It does not 755 | allow the child to continue processing as its parent process terminates. It is generally 756 | initiated by OS. 757 | 758 | #### 30.   What is starvation and aging in OS? 759 | 760 | **Starvation:** It is generally a problem that usually occurs when a process has not been 761 | able to get the required resources it needs for progress with its execution for a long 762 | period of time. In this condition, low priority processes get blocked and only high 763 | priority processes proceed towards completion because of which low priority 764 | processes suffer from lack of resources. 765 | 766 | **Aging:** It is a technique that is used to overcome the situation or problem of 767 | starvation. It simply increases the priority of processes that wait in the system for 768 | resources for a long period of time. It is considered the best technique to resolve the 769 | problem of starvation as it adds an aging factor to the priority of each and every 770 | request by various processes for resources. It also ensures that low-level queue jobs 771 | or processes complete their execution. 772 | 773 | ### Advanced OS Interview Questions 774 | 775 | #### 31.   What do you mean by Semaphore in OS? Why is it used? 776 | 777 | Semaphore is a signaling mechanism. It only holds one positive integer value. It is 778 | simply used to solve the problem or issue of critical sections in the synchronization 779 | process by using two atomic operations i.e., wait() and signal(). 780 | 781 | **Types of Semaphore** 782 | There are usually two types of semaphores as given below: 783 | 784 | ``` 785 | Binary Semaphore 786 | Counting Semaphore 787 | ``` 788 | 789 | ``` 790 | Binary Semaphore Mutex 791 | ``` 792 | 793 | ``` 794 | It allows various process threads 795 | to get the finite instance of the 796 | resource until resources are 797 | available. 798 | ``` 799 | 800 | ``` 801 | It allows various process 802 | threads to get single 803 | shared resource only at 804 | a time. 805 | ``` 806 | 807 | ``` 808 | Its functions are based upon 809 | signaling mechanisms. 810 | ``` 811 | 812 | ``` 813 | Its functions are based 814 | upon a locking 815 | mechanism. 816 | ``` 817 | 818 | ``` 819 | Binary semaphores are much 820 | faster as compared to Mutex. 821 | ``` 822 | 823 | ``` 824 | Mutex is slower as 825 | compared to binary 826 | semaphores. 827 | ``` 828 | 829 | ``` 830 | It is basically an integer. It is basically an object. 831 | ``` 832 | 833 | #### 32.   What is Kernel and write its main functions? 834 | 835 | The kernel is basically a computer program usually considered as a central 836 | component or module of OS. It is responsible for handling, managing, and 837 | controlling all operations of computer systems and hardware. Whenever the system 838 | starts, the kernel is loaded first and remains in the main memory. It also acts as an 839 | interface between user applications and hardware. 840 | 841 | **Functions of Kernel:** 842 | 843 | ``` 844 | It is responsible for managing all computer resources such as CPU, memory, 845 | files, processes, etc. 846 | It facilitates or initiates the interaction between components of hardware and 847 | soware. 848 | It manages RAM memory so that all running processes and programs can work 849 | effectively and efficiently. 850 | It also controls and manages all primary tasks of the OS as well as manages 851 | access and use of various peripherals connected to the computer. 852 | It schedules the work done by the CPU so that the work of each user is executed 853 | as efficiently as possible. 854 | ``` 855 | 856 | #### 33.   What are different types of Kernel? 857 | 858 | There are basically five types of Kernels as given below: 859 | 860 | ``` 861 | Monolithic Kernel 862 | MicroKernel 863 | Hybrid Kernel  864 | Nano Kernel 865 | Exo Kernel 866 | ``` 867 | 868 | #### 34.   Write difference between micro kernel and monolithic 869 | 870 | #### kernel? 871 | 872 | **MicroKernel** : It is a minimal OS that executes only important functions of OS. It only 873 | contains a near-minimum number of features and functions that are required to 874 | implement OS.  875 | Example: QNX, Mac OS X, K42, etc. 876 | 877 | **Monolithic Kernel:** It is an OS architecture that supports all basic features of 878 | computer components such as resource management, memory, file, etc.  879 | Example: Solaris, DOS, OpenVMS, Linux, etc. 880 | 881 | ``` 882 | MicroKernel Monolithic Kernel 883 | ``` 884 | 885 | ``` 886 | In this soware or program, 887 | kernel services and user 888 | services are present in 889 | different address spaces. 890 | ``` 891 | 892 | ``` 893 | In this soware or program, 894 | kernel services and user 895 | services are usually present in 896 | the same address space. 897 | ``` 898 | 899 | ``` 900 | It is smaller in size as 901 | compared to the 902 | monolithic kernel. 903 | ``` 904 | 905 | ``` 906 | It is larger in size as compared 907 | to a microkernel. 908 | ``` 909 | 910 | ``` 911 | It is easily extendible as 912 | compared to a monolithic 913 | kernel. 914 | ``` 915 | 916 | ``` 917 | It is hard to as extend as 918 | compared to a microkernel. 919 | ``` 920 | 921 | ``` 922 | If a service crashes, it does 923 | affect on working of the 924 | microkernel. 925 | ``` 926 | 927 | ``` 928 | If a service crashes, the whole 929 | system crashes in a 930 | monolithic kernel. 931 | ``` 932 | 933 | ``` 934 | It uses message queues to 935 | achieve inter-process 936 | communication. 937 | ``` 938 | 939 | ``` 940 | It uses signals and sockets to 941 | achieve inter-process 942 | communication. 943 | ``` 944 | 945 | #### 35.   What is SMP (Symmetric Multiprocessing)? 946 | 947 | SMP is generally referred to as computer architecture in which the processing of 948 | programs is done by multiple processors that share a common OS and memory. SMP 949 | is very much required if you want to take advantage of multiprocessor hardware. It 950 | simply enables any processor to work on any of the tasks no matter where data or 951 | resources for that particular task are located in memory. These systems are more 952 | reliable than single-processor systems. 953 | 954 | #### 36.   What is a time-sharing system? 955 | 956 | It is a system that allows more than one user to access the resources of a particular 957 | system in many locations. In simple words, it performs multiple tasks on a single 958 | processor or CPU. As the name suggests, it means to share time into multiple slots in 959 | several processes. It also allows different users from different locations to use a 960 | particular computer system at the same time therefore it is considered one of the 961 | important types of OS. 962 | 963 | #### 37.   What is Context Switching? 964 | 965 | Context switching is basically a process of saving the context of one process and 966 | loading the context of another process. It is one of the cost-effective and time-saving 967 | measures executed by CPU the because it allows multiple processes to share a single 968 | CPU. Therefore, it is considered an important part of a modern OS. This technique is 969 | used by OS to switch a process from one state to another i.e., from running state to 970 | ready state. It also allows a single CPU to handle and control various different 971 | processes or threads without even the need for additional resources. 972 | 973 | #### 38.   What is difference between Kernel and OS? 974 | 975 | **Kernel:** Kernel is a system program that controls all programs running on the 976 | computer. The kernel is basically a bridge between the soware and hardware of the 977 | system. 978 | 979 | **Operating System:** Operating system is a system program that runs on the computer 980 | to provide an interface to the computer user so that they can easily operate on the 981 | computer. 982 | 983 | ``` 984 | Kernel OS 985 | ``` 986 | 987 | ``` 988 | It is considered a central 989 | component of OS 990 | ``` 991 | 992 | ``` 993 | It is considered system soware. 994 | ``` 995 | 996 | ``` 997 | It is generally 998 | responsible for 999 | converting user 1000 | commands into 1001 | machine-level 1002 | commands. 1003 | ``` 1004 | 1005 | ``` 1006 | It is generally responsible for 1007 | managing the resources of 1008 | system. 1009 | ``` 1010 | 1011 | ``` 1012 | It simply acts as an 1013 | interface between 1014 | hardware and 1015 | applications. 1016 | ``` 1017 | 1018 | ``` 1019 | It simply acts as an interface 1020 | between hardware and user. 1021 | ``` 1022 | 1023 | ``` 1024 | It also performs 1025 | functions like process 1026 | management, file 1027 | management, device 1028 | management, I/O 1029 | communication, etc. 1030 | ``` 1031 | 1032 | ``` 1033 | It also performs functions like 1034 | providing security to data and 1035 | files in the the system, providing 1036 | access controls to users, 1037 | maintaining the system privacy, 1038 | etc. 1039 | ``` 1040 | 1041 | ``` 1042 | Its type includes 1043 | Microkernel, Monolithic 1044 | kernel, etc. 1045 | ``` 1046 | 1047 | ``` 1048 | Its type includes Single and 1049 | Multiprogramming batch 1050 | systems, Distributed OS, Real- 1051 | time OS. 1052 | ``` 1053 | 1054 | #### 39.   What is difference between process and thread? 1055 | 1056 | **Process:** It is basically a program that is currently under execution by one or more 1057 | threads. It is a very important part of the modern-day OS. 1058 | 1059 | **Thread:** It is a path of execution that is composed of the program counter, thread id, 1060 | stack, and set of registers within the process. 1061 | 1062 | **Process Thread** 1063 | 1064 | It is a computer program 1065 | that is under execution. 1066 | 1067 | ``` 1068 | It is the component or entity 1069 | of the process that is the 1070 | smallest execution unit. 1071 | ``` 1072 | 1073 | These are heavy-weight 1074 | operators. 1075 | 1076 | ``` 1077 | These are lightweight 1078 | operators. 1079 | ``` 1080 | 1081 | It has its own memory space. It uses the memory of theprocess they belong to. 1082 | 1083 | It is more difficult to create a 1084 | process as compared to 1085 | creating a thread. 1086 | 1087 | ``` 1088 | It is easier to create a thread 1089 | as compared to creating a 1090 | process. 1091 | ``` 1092 | 1093 | It requires more resources as 1094 | compared to thread. 1095 | 1096 | ``` 1097 | It requires fewer resources as 1098 | compared to processes. 1099 | ``` 1100 | 1101 | It takes more time to create 1102 | and terminate a process as 1103 | compared to a thread. 1104 | 1105 | ``` 1106 | It takes less time to create 1107 | and terminate a thread as 1108 | compared to a process. 1109 | ``` 1110 | 1111 | It usually run-in separate 1112 | memory space. 1113 | 1114 | ``` 1115 | It usually run-in shared 1116 | memory space. 1117 | ``` 1118 | 1119 | It does not share data. It shares data with eachother. 1120 | 1121 | It can be divided into 1122 | multiple threads. 1123 | 1124 | ``` 1125 | It can’t be further 1126 | subdivided. 1127 | ``` 1128 | 1129 | #### 40.   What are various sections of the process? 1130 | 1131 | There are basically four sections in the process as given below: 1132 | 1133 | ``` 1134 | Stack: It is used for local variables and returns addresses.  1135 | Heap: It is used for dynamic memory allocation. 1136 | Data : It stores global and static variables. 1137 | Code or text: It comprises compiled program code. 1138 | ``` 1139 | 1140 | #### 41.   What is a deadlock in OS? What are the necessary conditions 1141 | 1142 | #### for a deadlock? 1143 | 1144 | Deadlock is generally a situation where a set of processes are blocked as each process 1145 | is holding resources and waits to acquire resources held by another process. In this 1146 | situation, two or more processes simply try to execute simultaneously and wait for 1147 | each to finish their execution because they are dependent on each other. We can see 1148 | a hand problem in our system whenever a deadlock occurs in a program. It is one of 1149 | the common problems you can see in multiprocessing. 1150 | 1151 | **Necessary Conditions for Deadlock** 1152 | 1153 | There are basically four necessary conditions for deadlock as given below: 1154 | 1155 | ``` 1156 | Mutual Exclusion 1157 | Hold and Wait 1158 | No Pre-emption 1159 | Circular Wait or Resource Wait 1160 | ``` 1161 | 1162 | #### 42.   What do you mean by Belady’s Anomaly? 1163 | 1164 | In the Operating System, process data is loaded in fixed-sized chunks and each chunk 1165 | is referred to as a page. The processor loads these pages in the fixed-sized chunks of 1166 | memory called frames. Belady’s Anomaly is a phenomenon in which if we increase 1167 | the number of frames in memory, then the number of page faults also increases. It is 1168 | generally experienced when we use FIFO (First in First out) page replacement 1169 | algorithm. 1170 | 1171 | #### 43.   What is spooling in OS? 1172 | 1173 | Spooling simply stands for Simultaneous peripheral operations online. It is referred 1174 | to as putting data of various I/O jobs in a buffer. Here, buffer means a special area in 1175 | memory or hard disk that can be accessible to an I/O device. It is used for mediation 1176 | between a computer application and a slow peripheral. It is very useful and 1177 | important because devices access or acquire data at different rates. This operation 1178 | also uses disk as a very large buffer and is capable of overlapping I/O operations for 1179 | one task with processor operations for another task. 1180 | --------------------------------------------------------------------------------