{"id":12598,"date":"2024-09-10T10:00:30","date_gmt":"2024-09-10T04:30:30","guid":{"rendered":"https:\/\/www.placementpreparation.io\/blog\/?p=12598"},"modified":"2026-02-19T13:56:59","modified_gmt":"2026-02-19T08:26:59","slug":"dsa-interview-questions-for-freshers","status":"publish","type":"post","link":"https:\/\/www.placementpreparation.io\/blog\/dsa-interview-questions-for-freshers\/","title":{"rendered":"Top DSA Interview Questions for Freshers"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>Are you preparing for your first Data Structures and Algorithms interview and wondering what questions you might face?<\/p><p>Understanding the key Data Structures and Algorithms interview questions for freshers can give you more clarity.<\/p><p>With this guide, you&rsquo;ll be well-prepared to tackle these Data Structures and Algorithms interview questions and answers for freshers and make a strong impression in your interview.<\/p><p><a href=\"https:\/\/www.guvi.in\/courses\/database-and-cloud-computing\/dsa-using-python\/?utm_source=placement_preparation&amp;utm_medium=blog_banner&amp;utm_campaign=dsa_interview_questions_for_freshers_horizontal\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone wp-image-10355 size-full\" src=\"https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/dsa-using-python-course-desktop-banner-horizontal.webp\" alt=\"dsa using python course desktop banner horizontal\" width=\"2270\" height=\"600\" srcset=\"https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/dsa-using-python-course-desktop-banner-horizontal.webp 2270w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/dsa-using-python-course-desktop-banner-horizontal-300x79.webp 300w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/dsa-using-python-course-desktop-banner-horizontal-1024x271.webp 1024w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/dsa-using-python-course-desktop-banner-horizontal-768x203.webp 768w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/dsa-using-python-course-desktop-banner-horizontal-1536x406.webp 1536w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/dsa-using-python-course-desktop-banner-horizontal-2048x541.webp 2048w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/dsa-using-python-course-desktop-banner-horizontal-150x40.webp 150w\" sizes=\"(max-width: 2270px) 100vw, 2270px\"><\/a><\/p><h2 id=\"practice-dsa-interview-questions\">Practice Data Structures &amp; Algorithms Interview Questions<\/h2><p>Below are the Data Structures &amp; Algorithms interview questions for freshers with answers:<\/p><h3 id=\"reverse-string-python\">1. How do you reverse a string in Python?<\/h3><p><strong>Answer:<\/strong><\/p><p>You can reverse a string using Python slicing. The syntax is <strong>str[::-1]<\/strong>, which iterates over the string in reverse.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def reverse_string(s):<br>\nreturn s[::-1]\n<\/p><\/div><\/div><h3 id=\"find-max-in-array\">2. How do you find the maximum element in an array?<\/h3><p><strong>Answer:<\/strong><\/p><p>You can find the maximum element in an array using Python&rsquo;s built-in <strong>max()<\/strong> function.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>arr = [1, 4, 5, 2]\nmax_element = max(arr)<\/p>\n<\/div><\/div><h3 id=\"check-palindrome-string\">3. How do you check if a string is a palindrome?<\/h3><p><strong>Answer:<\/strong><\/p><p>A string is a palindrome if it reads the same forward and backward. You can check this by comparing the string with its reverse.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def is_palindrome(s):<br>\nreturn s == s[::-1]\n<\/p><\/div><\/div><h3 id=\"second-largest-in-array\">4. How do you find the second largest element in an array?<\/h3><p><strong>Answer:<\/strong><\/p><p>Sort the array in descending order and return the second element. Alternatively, iterate through the array to find the second largest element.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def second_largest(arr):<br>\narr = list(set(arr)) # Remove duplicates<br>\narr.sort(reverse=True)<br>\nreturn arr[1]\n<\/p><\/div><\/div><h3 id=\"sum-of-digits\">5. How do you find the sum of digits of a given number?<\/h3><p><strong>Answer:<\/strong><\/p><p>To find the sum of digits, convert the number to a string and sum the individual digits using sum(map(int, str(n))).<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def sum_of_digits(n):<br>\nreturn sum(int(digit) for digit in str(n))<\/p>\n<\/div><\/div><p class=\"related\"><strong>Practice Now:<\/strong> <a href=\"https:\/\/www.placementpreparation.io\/dsa\/?utm_source=placement_preparation&amp;utm_medium=blog_cta&amp;utm_campaign=dsa_interview_questions_for_freshers\">Explore topic-wise DSA practice questions for placements<\/a><strong><br>\n<\/strong><\/p><h3 id=\"print-fibonacci-series\">6. How do you print the Fibonacci series up to n terms?<\/h3><p><strong>Answer:<\/strong><\/p><p>You can calculate the Fibonacci series iteratively by adding the two previous numbers to get the next one.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def fibonacci(n):<br>\na, b = 0, 1<br>\nfor _ in range(n):<br>\nprint(a, end=&rsquo; &lsquo;)<br>\na, b = b, a + b<\/p>\n<\/div><\/div><h3 id=\"find-factorial-number\">7. How do you find the factorial of a given number n?<\/h3><p><strong>Answer:<\/strong><\/p><p>The factorial of a number is the product of all positive integers less than or equal to <strong>n<\/strong>. This can be computed using recursion.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def factorial(n):<br>\nreturn 1 if n == 0 else n * factorial(n &ndash; 1)<\/p>\n<\/div><\/div><h3 id=\"missing-number-sequence\">8. How do you find the missing number in a sequence from 1 to n?<\/h3><p><strong>Answer:<\/strong><\/p><p>The sum of numbers from 1 to <strong>n<\/strong> is <strong>n*(n+1)\/\/2<\/strong>. Find the difference between this sum and the sum of elements in the array.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def find_missing(arr, n):<br>\ntotal = n * (n + 1) \/\/ 2<br>\nreturn total &ndash; sum(arr)<\/p>\n<\/div><\/div><h3 id=\"check-anagram-strings\">9. How do you check if two strings are anagrams?<\/h3><p><strong>Answer:<\/strong><\/p><p>Two strings are anagrams if they contain the same characters in different orders. Sort both strings and compare them.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def are_anagrams(str1, str2):<br>\nreturn sorted(str1) == sorted(str2)<\/p>\n<\/div><\/div><h3 id=\"find-duplicate-in-array\">10. How do you find a duplicate element in an array?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a set to keep track of elements. If an element already exists in the set, it&rsquo;s a duplicate.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def find_duplicate(arr):<br>\nseen = set()<br>\nfor num in arr:<br>\nif num in seen:<br>\nreturn num<br>\nseen.add(num)<\/p>\n<\/div><\/div><h3 id=\"longest-unique-substring\">11. How do you find the length of the longest substring without repeating characters?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a sliding window approach to track the longest substring with unique characters.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def longest_unique_substring(s):<br>\nchar_set = set()<br>\nl, res = 0, 0<br>\nfor r in range(len(s)):<br>\nwhile s[r] in char_set:<br>\nchar_set.remove(s[l])<br>\nl += 1<br>\nchar_set.add(s[r])<br>\nres = max(res, r &ndash; l + 1)<br>\nreturn res<\/p>\n<\/div><\/div><h3 id=\"intersection-of-arrays\">12. How do you find the intersection of two arrays?<\/h3><p><strong>Answer:<\/strong><\/p><p>Convert both arrays to sets and use the &amp; operator to find the intersection.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def intersection(arr1, arr2):<br>\nreturn list(set(arr1) &amp; set(arr2))<\/p>\n<\/div><\/div><h3 id=\"height-balanced-binary-tree\">13. How do you check if a binary tree is height-balanced?<\/h3><p><strong>Answer:<\/strong><\/p><p>A binary tree is balanced if the height difference between the left and right subtree is not more than 1.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def is_balanced(root):<br>\ndef height(node):<br>\nif not node:<br>\nreturn 0<br>\nleft = height(node.left)<br>\nright = height(node.right)<br>\nif left == -1 or right == -1 or abs(left &ndash; right) &gt; 1:<br>\nreturn -1<br>\nreturn 1 + max(left, right)<br>\nreturn height(root) != -1<\/p>\n<\/div><\/div><h3 id=\"implement-binary-search\">14. How do you implement binary search in a sorted array?<\/h3><p><strong>Answer:<\/strong><\/p><p>Binary search works by dividing the search space in half, checking the middle element, and deciding which half to search next.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def binary_search(arr, target):<br>\nlow, high = 0, len(arr) &ndash; 1<br>\nwhile low &lt;= high:<br>\nmid = (low + high) \/\/ 2<br>\nif arr[mid] == target:<br>\nreturn mid<br>\nelif arr[mid] &lt; target:<br>\nlow = mid + 1<br>\nelse:<br>\nhigh = mid &ndash; 1<br>\nreturn -1<\/p>\n<\/div><\/div><h3 id=\"merge-sorted-arrays\">15. How do you merge two sorted arrays into one sorted array?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use two pointers to iterate through both arrays and append the smaller element to the result.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def merge_sorted_arrays(arr1, arr2):<br>\ni, j = 0, 0<br>\nresult = []\nwhile i &lt; len(arr1) and j &lt; len(arr2):<br>\nif arr1[i] &lt; arr2[j]:<br>\nresult.append(arr1[i])<br>\ni += 1<br>\nelse:<br>\nresult.append(arr2[j])<br>\nj += 1<br>\nresult.extend(arr1[i:])<br>\nresult.extend(arr2[j:])<br>\nreturn result<\/p>\n<\/div><\/div><h3 id=\"middle-of-linked-list\">16. How do you find the middle element of a linked list?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use two pointers, one moving twice as fast as the other. When the fast pointer reaches the end, the slow pointer will be at the middle.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def find_middle(head):<br>\nslow, fast = head, head<br>\nwhile fast and fast.next:<br>\nslow = slow.next<br>\nfast = fast.next.next<br>\nreturn slow<\/p>\n<\/div><\/div><h3 id=\"check-prime-number\">17. How do you check if a number is prime?<\/h3><p><strong>Answer:<\/strong><\/p><p>A prime number has no divisors other than 1 and itself. Check divisibility up to the square root of the number.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def is_prime(n):<br>\nif n &lt;= 1:<br>\nreturn False<br>\nfor i in range(2, int(n**0.5) + 1):<br>\nif n % i == 0:<br>\nreturn False<br>\nreturn True<\/p>\n<\/div><\/div><h3 id=\"greatest-common-divisor\">18. How do you find the greatest common divisor (GCD) of two numbers?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use Euclid&rsquo;s algorithm: repeatedly divide the larger number by the smaller until the remainder is zero.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def gcd(a, b):<br>\nwhile b:<br>\na, b = b, a % b<br>\nreturn a<\/p>\n<\/div><\/div><h3 id=\"merge-overlapping-intervals\">19. How do you merge overlapping intervals?<\/h3><p><strong>Answer:<\/strong><\/p><p>Sort the intervals by the start time, then iterate through them and merge overlapping intervals.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def merge_intervals(intervals):<br>\nintervals.sort(key=lambda x: x[0])<br>\nmerged = [intervals[0]]\nfor curr in intervals[1:]:<br>\nif curr[0] &lt;= merged[-1][1]:<br>\nmerged[-1][1] = max(merged[-1][1], curr[1])<br>\nelse:<br>\nmerged.append(curr)<br>\nreturn merged<\/p>\n<\/div><\/div><h3 id=\"kth-largest-element\">20. How do you find the Kth largest element in an array?<\/h3><p><strong>Answer:<\/strong><\/p><p>Sort the array and return the Kth largest element. Alternatively, use a min-heap.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>import heapq<\/p>\n<p>def kth_largest(arr, k):<br>\nreturn heapq.nlargest(k, arr)[-1]\n<\/p><\/div><\/div><h3 id=\"longest-palindromic-substring\">21. How do you find the longest palindromic substring in a given string?<\/h3><p><strong>Answer:<\/strong><\/p><p>Expand around the center of the string and check for the longest palindrome in both even and odd-length substrings.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def longest_palindrome(s):<br>\ndef expand_around_center(left, right):<br>\nwhile left &gt;= 0 and right &lt; len(s) and s[left] == s[right]:<br>\nleft -= 1<br>\nright += 1<br>\nreturn right &ndash; left &ndash; 1<\/p>\n<p>start, end = 0, 0<br>\nfor i in range(len(s)):<br>\nlen1 = expand_around_center(i, i)<br>\nlen2 = expand_around_center(i, i + 1)<br>\nmax_len = max(len1, len2)<br>\nif max_len &gt; end &ndash; start:<br>\nstart = i &ndash; (max_len &ndash; 1) \/\/ 2<br>\nend = i + max_len \/\/ 2<br>\nreturn s[start:end + 1]\n<\/p><\/div><\/div><h3 id=\"balanced-parentheses-check\">22. How do you check if a string containing parentheses is balanced?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a stack to keep track of open parentheses. For each closing parenthesis, pop the stack and check for a matching opening parenthesis.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def is_balanced(s):<br>\nstack = []\nmatching_parentheses = {&lsquo;)&rsquo;: &lsquo;(&lsquo;, &lsquo;]&rsquo;: &lsquo;[&lsquo;, &lsquo;}&rsquo;: &lsquo;{&lsquo;}<br>\nfor char in s:<br>\nif char in matching_parentheses.values():<br>\nstack.append(char)<br>\nelif char in matching_parentheses:<br>\nif not stack or stack.pop() != matching_parentheses[char]:<br>\nreturn False<br>\nreturn not stack<\/p>\n<\/div><\/div><h3 id=\"subarray-with-sum\">23. How do you find a subarray with a given sum in a non-negative array?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the sliding window technique to adjust the window size dynamically until the target sum is found.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def find_subarray_with_sum(arr, target):<br>\nstart, current_sum = 0, 0<br>\nfor end in range(len(arr)):<br>\ncurrent_sum += arr[end]\nwhile current_sum &gt; target:<br>\ncurrent_sum -= arr[start]\nstart += 1<br>\nif current_sum == target:<br>\nreturn arr[start:end + 1]\nreturn []\n<\/p><\/div><\/div><h3 id=\"minimum-in-rotated-array\">24. How do you find the minimum element in a rotated sorted array?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use binary search to find the inflection point where the array is rotated.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def find_min_in_rotated_array(arr):<br>\nlow, high = 0, len(arr) &ndash; 1<br>\nwhile low &lt; high:<br>\nmid = (low + high) \/\/ 2<br>\nif arr[mid] &gt; arr[high]:<br>\nlow = mid + 1<br>\nelse:<br>\nhigh = mid<br>\nreturn arr[low]\n<\/p><\/div><\/div><h3 id=\"median-of-sorted-arrays\">25. How do you find the median of two sorted arrays?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use binary search on the smaller array to partition both arrays into two halves, ensuring that the left half contains smaller elements than the right half.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def find_median_sorted_arrays(nums1, nums2):<br>\nif len(nums1) &gt; len(nums2):<br>\nnums1, nums2 = nums2, nums1<br>\nx, y = len(nums1), len(nums2)<br>\nlow, high = 0, x<br>\nwhile low &lt;= high:<br>\npartitionX = (low + high) \/\/ 2<br>\npartitionY = (x + y + 1) \/\/ 2 &ndash; partitionX<\/p>\n<p>maxX = float(&lsquo;-inf&rsquo;) if partitionX == 0 else nums1[partitionX &ndash; 1]\nminX = float(&lsquo;inf&rsquo;) if partitionX == x else nums1[partitionX]\n<\/p><p>maxY = float(&lsquo;-inf&rsquo;) if partitionY == 0 else nums2[partitionY &ndash; 1]\nminY = float(&lsquo;inf&rsquo;) if partitionY == y else nums2[partitionY]\n<\/p><p>if maxX &lt;= minY and maxY &lt;= minX:<br>\nif (x + y) % 2 == 0:<br>\nreturn (max(maxX, maxY) + min(minX, minY)) \/ 2<br>\nelse:<br>\nreturn max(maxX, maxY)<br>\nelif maxX &gt; minY:<br>\nhigh = partitionX &ndash; 1<br>\nelse:<br>\nlow = partitionX + 1<\/p>\n<\/div><\/div><h3 id=\"queue-using-two-stacks\">26. How do you implement a queue using two stacks?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use two stacks: one for enqueuing elements and another for dequeuing.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>class QueueUsingStacks:<br>\ndef __init__(self):<br>\nself.stack1 = []\nself.stack2 = []\n<\/p><p>def enqueue(self, x):<br>\nself.stack1.append(x)<\/p>\n<p>def dequeue(self):<br>\nif not self.stack2:<br>\nwhile self.stack1:<br>\nself.stack2.append(self.stack1.pop())<br>\nreturn self.stack2.pop()<\/p>\n<\/div><\/div><h3 id=\"symmetric-binary-tree\">27. How do you check if a binary tree is symmetric?<\/h3><p><strong>Answer:<\/strong><\/p><p>A binary tree is symmetric if the left subtree is a mirror reflection of the right subtree.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def is_symmetric(root):<br>\ndef is_mirror(t1, t2):<br>\nif not t1 and not t2:<br>\nreturn True<br>\nif not t1 or not t2:<br>\nreturn False<br>\nreturn t1.val == t2.val and is_mirror(t1.left, t2.right) and is_mirror(t1.right, t2.left)<br>\nreturn is_mirror(root, root)<\/p>\n<\/div><\/div><h3 id=\"lowest-common-ancestor\">28. How do you find the lowest common ancestor of two nodes in a binary tree?<\/h3><p><strong>Answer:<\/strong><\/p><p>The LCA is the node that is the deepest common ancestor of both nodes. Traverse the tree recursively, and return the node where the paths to both nodes diverge.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def lowest_common_ancestor(root, p, q):<br>\nif not root or root == p or root == q:<br>\nreturn root<br>\nleft = lowest_common_ancestor(root.left, p, q)<br>\nright = lowest_common_ancestor(root.right, p, q)<br>\nif left and right:<br>\nreturn root<br>\nreturn left if left else right<\/p>\n<\/div><\/div><h3 id=\"count-islands-in-grid\">29. How do you count the number of islands in a 2D grid?<\/h3><p><strong>Answer:<\/strong><\/p><p>Perform a depth-first search (DFS) to mark all connected components of land (1) as visited.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def num_islands(grid):<br>\nif not grid:<br>\nreturn 0<\/p>\n<p>def dfs(grid, i, j):<br>\nif i &lt; 0 or i &gt;= len(grid) or j &lt; 0 or j &gt;= len(grid[0]) or grid[i][j] == &lsquo;0&rsquo;:<br>\nreturn<br>\ngrid[i][j] = &lsquo;0&rsquo;<br>\ndfs(grid, i + 1, j)<br>\ndfs(grid, i &ndash; 1, j)<br>\ndfs(grid, i, j + 1)<br>\ndfs(grid, i, j &ndash; 1)<\/p>\n<p>count = 0<br>\nfor i in range(len(grid)):<br>\nfor j in range(len(grid[0])):<br>\nif grid[i][j] == &lsquo;1&rsquo;:<br>\ndfs(grid, i, j)<br>\ncount += 1<br>\nreturn count<\/p>\n<\/div><\/div><h3 id=\"maximum-subarray-sum\">30. How do you find the maximum sum of a subarray using Kadane&rsquo;s Algorithm?<\/h3><p><strong>Answer:<\/strong><\/p><p>Kadane&rsquo;s algorithm scans the array and computes the maximum subarray sum in linear time by updating the current maximum at each step.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def max_subarray_sum(arr):<br>\nmax_so_far = max_ending_here = arr[0]\nfor i in range(1, len(arr)):<br>\nmax_ending_here = max(arr[i], max_ending_here + arr[i])<br>\nmax_so_far = max(max_so_far, max_ending_here)<br>\nreturn max_so_far<\/p>\n<\/div><\/div><h3 id=\"stack-using-two-queues\">31. How do you implement a stack using two queues?<\/h3><p><strong>Answer:<\/strong><\/p><p>You can implement a stack by using two queues, one for pushing and the other for popping elements. When popping, the elements are dequeued until only one remains.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>from collections import deque<\/p>\n<p>class StackUsingQueues:<br>\ndef __init__(self):<br>\nself.queue1 = deque()<br>\nself.queue2 = deque()<\/p>\n<p>def push(self, x):<br>\nself.queue1.append(x)<\/p>\n<p>def pop(self):<br>\nwhile len(self.queue1) &gt; 1:<br>\nself.queue2.append(self.queue1.popleft())<br>\npopped = self.queue1.popleft()<br>\nself.queue1, self.queue2 = self.queue2, self.queue1<br>\nreturn popped<\/p>\n<p>def top(self):<br>\nreturn self.queue1[-1]\n<\/p><p>def empty(self):<br>\nreturn not self.queue1<\/p>\n<\/div><\/div><h3 id=\"permutations-of-string\">32. How do you find all permutations of a string?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use backtracking to generate all possible permutations of a string by swapping characters.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def permute(s):<br>\ndef backtrack(start):<br>\nif start == len(s) &ndash; 1:<br>\nresult.append(&rdquo;.join(s))<br>\nfor i in range(start, len(s)):<br>\ns[start], s[i] = s[i], s[start]\nbacktrack(start + 1)<br>\ns[start], s[i] = s[i], s[start]\n<\/p><p>result = []\ns = list(s)<br>\nbacktrack(0)<br>\nreturn result<\/p>\n<\/div><\/div><h3 id=\"kth-smallest-in-bst\">33. How do you find the Kth smallest element in a Binary Search Tree (BST)?<\/h3><p><strong>Answer:<\/strong><\/p><p>Inorder traversal of a BST gives the elements in sorted order. Perform an inorder traversal and stop at the Kth element.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def kth_smallest(root, k):<br>\ndef inorder(node):<br>\nif node:<br>\nyield from inorder(node.left)<br>\nyield node.val<br>\nyield from inorder(node.right)<\/p>\n<p>gen = inorder(root)<br>\nfor _ in range(k):<br>\nresult = next(gen)<br>\nreturn result<\/p>\n<\/div><\/div><h3 id=\"peak-element-array\">34. How do you find a peak element in an array where an element is greater than its neighbors?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use binary search to efficiently find a peak element.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def find_peak(arr):<br>\nlow, high = 0, len(arr) &ndash; 1<br>\nwhile low &lt; high:<br>\nmid = (low + high) \/\/ 2<br>\nif arr[mid] &lt; arr[mid + 1]:<br>\nlow = mid + 1<br>\nelse:<br>\nhigh = mid<br>\nreturn arr[low]\n<\/p><\/div><\/div><h3 id=\"count-1s-in-binary\">35. How do you count the number of 1&rsquo;s in the binary representation of an integer?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use bit manipulation by repeatedly clearing the least significant bit set to 1.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def count_ones(n):<br>\ncount = 0<br>\nwhile n:<br>\nn &amp;= n &ndash; 1<br>\ncount += 1<br>\nreturn count<\/p>\n<\/div><\/div><h3 id=\"convert-tree-to-dll\">36. How do you convert a binary tree to a doubly linked list in-place?<\/h3><p><strong>Answer:<\/strong><\/p><p>Perform an inorder traversal and adjust pointers as you visit each node to create a doubly linked list.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def convert_to_dll(root):<br>\ndef inorder(node):<br>\nnonlocal prev<br>\nif not node:<br>\nreturn<br>\ninorder(node.left)<br>\nif prev:<br>\nprev.right = node<br>\nnode.left = prev<br>\nprev = node<br>\ninorder(node.right)<\/p>\n<p>if not root:<br>\nreturn None<br>\nprev = None<br>\ninorder(root)<br>\nwhile root.left:<br>\nroot = root.left<br>\nreturn root<\/p>\n<\/div><\/div><h3 id=\"longest-increasing-subsequence\">37. How do you find the longest increasing subsequence in an array?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use dynamic programming to store the length of the longest subsequence that ends at each element.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def longest_increasing_subsequence(arr):<br>\ndp = [1] * len(arr)<br>\nfor i in range(1, len(arr)):<br>\nfor j in range(i):<br>\nif arr[i] &gt; arr[j]:<br>\ndp[i] = max(dp[i], dp[j] + 1)<br>\nreturn max(dp)<\/p>\n<\/div><\/div><h3 id=\"flatten-multilevel-list\">38. How do you flatten a multilevel doubly linked list where each node can have a child pointing to another doubly linked list?<\/h3><p><strong>Answer:<\/strong><\/p><p>Perform a depth-first traversal and adjust pointers to flatten the multilevel list.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def flatten(head):<br>\nif not head:<br>\nreturn head<br>\ndummy = Node(0)<br>\nprev = dummy<br>\nstack = [head]\n<\/p><p>while stack:<br>\ncurr = stack.pop()<br>\nprev.next = curr<br>\ncurr.prev = prev<br>\nif curr.next:<br>\nstack.append(curr.next)<br>\nif curr.child:<br>\nstack.append(curr.child)<br>\ncurr.child = None<br>\nprev = curr<\/p>\n<p>dummy.next.prev = None<br>\nreturn dummy.next<\/p>\n<\/div><\/div><h3 id=\"maximum-product-subarray\">39. How do you find the maximum product of a contiguous subarray?<\/h3><p><strong>Answer:<\/strong><\/p><p>Keep track of the current maximum and minimum products as you iterate through the array.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def max_product_subarray(nums):<br>\nmax_prod, min_prod, result = nums[0], nums[0], nums[0]\nfor i in range(1, len(nums)):<br>\ncandidates = (nums[i], nums[i] * max_prod, nums[i] * min_prod)<br>\nmax_prod = max(candidates)<br>\nmin_prod = min(candidates)<br>\nresult = max(result, max_prod)<br>\nreturn result<\/p>\n<\/div><\/div><h3 id=\"median-of-data-stream\">40. How do you find the median of a data stream?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use two heaps: a max-heap for the lower half and a min-heap for the upper half of the data.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>import heapq<\/p>\n<p>class MedianFinder:<br>\ndef __init__(self):<br>\nself.low = [] # Max-heap<br>\nself.high = [] # Min-heap<\/p>\n<p>def add_num(self, num):<br>\nheapq.heappush(self.low, -num)<br>\nheapq.heappush(self.high, -heapq.heappop(self.low))<br>\nif len(self.low) &lt; len(self.high):<br>\nheapq.heappush(self.low, -heapq.heappop(self.high))<\/p>\n<p>def find_median(self):<br>\nif len(self.low) &gt; len(self.high):<br>\nreturn -self.low[0]\nreturn (-self.low[0] + self.high[0]) \/ 2<\/p>\n<\/div><\/div><h3 id=\"unique-triplets-sum-zero\">41. How do you find all unique triplets in an array that sum to zero?<\/h3><p><strong>Answer:<\/strong><\/p><p>Sort the array and use two pointers to find pairs that sum to the negative of the current element.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def three_sum(nums):<br>\nnums.sort()<br>\nresult = []\nfor i in range(len(nums) &ndash; 2):<br>\nif i &gt; 0 and nums[i] == nums[i &ndash; 1]:<br>\ncontinue<br>\nleft, right = i + 1, len(nums) &ndash; 1<br>\nwhile left &lt; right:<br>\ntotal = nums[i] + nums[left] + nums[right]\nif total &lt; 0:<br>\nleft += 1<br>\nelif total &gt; 0:<br>\nright -= 1<br>\nelse:<br>\nresult.append([nums[i], nums[left], nums[right]])<br>\nwhile left &lt; right and nums[left] == nums[left + 1]:<br>\nleft += 1<br>\nwhile left &lt; right and nums[right] == nums[right &ndash; 1]:<br>\nright -= 1<br>\nleft += 1<br>\nright -= 1<br>\nreturn result<\/p>\n<\/div><\/div><h3 id=\"minimum-window-substring\">42. How do you find the minimum window substring in a string that contains all the characters of another string?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a sliding window and a hash map to track the frequency of required characters.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>from collections import Counter<\/p>\n<p>def min_window(s, t):<br>\nif not s or not t:<br>\nreturn &ldquo;&rdquo;<br>\ndict_t = Counter(t)<br>\nrequired = len(dict_t)<br>\nleft, right = 0, 0<br>\nformed = 0<br>\nwindow_counts = {}<br>\nans = float(&ldquo;inf&rdquo;), None, None<br>\nwhile right &lt; len(s):<br>\nchar = s[right]\nwindow_counts[char] = window_counts.get(char, 0) + 1<br>\nif char in dict_t and window_counts[char] == dict_t[char]:<br>\nformed += 1<br>\nwhile left &lt;= right and formed == required:<br>\nchar = s[left]\nif right &ndash; left + 1 &lt; ans[0]:<br>\nans = (right &ndash; left + 1, left, right)<br>\nwindow_counts[char] -= 1<br>\nif char in dict_t and window_counts[char] &lt; dict_t[char]:<br>\nformed -= 1<br>\nleft += 1<br>\nright += 1<br>\nreturn &ldquo;&rdquo; if ans[0] == float(&ldquo;inf&rdquo;) else s[ans[1]:ans[2] + 1]\n<\/p><\/div><\/div><h3 id=\"serialize-and-deserialize-tree\">43. How do you serialize and deserialize a binary tree?<\/h3><p><strong>Answer:<\/strong><\/p><p>Serialize the tree into a string using pre-order traversal and deserialize it back into a tree using a queue.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def serialize(root):<br>\ndef helper(node):<br>\nif not node:<br>\nresult.append(&lsquo;None&rsquo;)<br>\nelse:<br>\nresult.append(str(node.val))<br>\nhelper(node.left)<br>\nhelper(node.right)<\/p>\n<p>result = []\nhelper(root)<br>\nreturn &lsquo;,&rsquo;.join(result)<\/p>\n<p>def deserialize(data):<br>\ndef helper():<br>\nval = next(values)<br>\nif val == &lsquo;None&rsquo;:<br>\nreturn None<br>\nnode = TreeNode(int(val))<br>\nnode.left = helper()<br>\nnode.right = helper()<br>\nreturn node<\/p>\n<p>values = iter(data.split(&lsquo;,&rsquo;))<br>\nreturn helper()<\/p>\n<\/div><\/div><h3 id=\"lru-cache-implementation\">44. How do you implement an LRU (Least Recently Used) Cache?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a combination of a dictionary and a doubly linked list to keep track of the least recently used elements.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>class LRUCache:<br>\ndef __init__(self, capacity):<br>\nself.cache = {}<br>\nself.capacity = capacity<br>\nself.head = ListNode(0)<br>\nself.tail = ListNode(0)<br>\nself.head.next = self.tail<br>\nself.tail.prev = self.head<\/p>\n<p>def _add(self, node):<br>\np = self.head.next<br>\nself.head.next = node<br>\nnode.prev = self.head<br>\nnode.next = p<br>\np.prev = node<\/p>\n<p>def _remove(self, node):<br>\np = node.prev<br>\nn = node.next<br>\np.next = n<br>\nn.prev = p<\/p>\n<p>def get(self, key):<br>\nif key in self.cache:<br>\nnode = self.cache[key]\nself._remove(node)<br>\nself._add(node)<br>\nreturn node.val<br>\nreturn -1<\/p>\n<p>def put(self, key, value):<br>\nif key in self.cache:<br>\nself._remove(self.cache[key])<br>\nnode = ListNode(key, value)<br>\nself.cache[key] = node<br>\nself._add(node)<br>\nif len(self.cache) &gt; self.capacity:<br>\nn = self.tail.prev<br>\nself._remove(n)<br>\ndel self.cache[n.key]\n<\/p><\/div><\/div><h3 id=\"maximum-path-sum\">45. How do you find the maximum path sum in a binary tree?<\/h3><p><strong>Answer:<\/strong><\/p><p>Recursively calculate the maximum gain from each node and update the global maximum path sum.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def max_path_sum(root):<br>\ndef helper(node):<br>\nnonlocal max_sum<br>\nif not node:<br>\nreturn 0<br>\nleft_gain = max(helper(node.left), 0)<br>\nright_gain = max(helper(node.right), 0)<br>\nmax_sum = max(max_sum, node.val + left_gain + right_gain)<br>\nreturn node.val + max(left_gain, right_gain)<\/p>\n<p>max_sum = float(&lsquo;-inf&rsquo;)<br>\nhelper(root)<br>\nreturn max_sum<\/p>\n<\/div><\/div><h3 id=\"longest-common-subsequence\">46. How do you find the longest common subsequence (LCS) between two strings?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use dynamic programming to store the LCS length at each substring.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def longest_common_subsequence(text1, text2):<br>\ndp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]\nfor i in range(1, len(text1) + 1):<br>\nfor j in range(1, len(text2) + 1):<br>\nif text1[i &ndash; 1] == text2[j &ndash; 1]:<br>\ndp[i][j] = dp[i &ndash; 1][j &ndash; 1] + 1<br>\nelse:<br>\ndp[i][j] = max(dp[i &ndash; 1][j], dp[i][j &ndash; 1])<br>\nreturn dp[-1][-1]\n<\/p><\/div><\/div><h3 id=\"shortest-path-dijkstra\">47. How do you find the shortest path in a graph using Dijkstra&rsquo;s Algorithm?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a priority queue to greedily select the node with the smallest distance, and update the distances of its neighbors.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>import heapq<\/p>\n<p>def dijkstra(graph, start):<br>\ndistances = {node: float(&lsquo;infinity&rsquo;) for node in graph}<br>\ndistances[start] = 0<br>\npq = [(0, start)]\n<\/p><p>while pq:<br>\ncurrent_distance, current_node = heapq.heappop(pq)<br>\nif current_distance &gt; distances[current_node]:<br>\ncontinue<br>\nfor neighbor, weight in graph[current_node]:<br>\ndistance = current_distance + weight<br>\nif distance &lt; distances[neighbor]:<br>\ndistances[neighbor] = distance<br>\nheapq.heappush(pq, (distance, neighbor))<\/p>\n<p>return distances<\/p>\n<\/div><\/div><h3 id=\"count-inversions-array\">48. How do you count the number of inversions in an array (where i &lt; j and arr[i] &gt; arr[j])?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a modified merge sort algorithm to count the inversions while sorting the array.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def merge_sort_and_count(arr):<br>\nif len(arr) &lt; 2:<br>\nreturn arr, 0<br>\nmid = len(arr) \/\/ 2<br>\nleft, left_inv = merge_sort_and_count(arr[:mid])<br>\nright, right_inv = merge_sort_and_count(arr[mid:])<br>\nmerged, split_inv = merge_and_count(left, right)<br>\nreturn merged, left_inv + right_inv + split_inv<\/p>\n<p>def merge_and_count(left, right):<br>\nresult = []\ni = j = inv_count = 0<br>\nwhile i &lt; len(left) and j &lt; len(right):<br>\nif left[i] &lt;= right[j]:<br>\nresult.append(left[i])<br>\ni += 1<br>\nelse:<br>\nresult.append(right[j])<br>\nj += 1<br>\ninv_count += len(left) &ndash; i<br>\nresult += left[i:]\nresult += right[j:]\nreturn result, inv_count<\/p>\n<\/div><\/div><h3 id=\"check-power-of-two\">49. How do you check if a given number is a power of two?<\/h3><p><strong>Answer:<\/strong><\/p><p>A number is a power of two if it has only one bit set in its binary representation.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>def is_power_of_two(n):<br>\nreturn n &gt; 0 and (n &amp; (n &ndash; 1)) == 0<\/p>\n<\/div><\/div><h3 id=\"implement-a-trie\">50. How do you implement a Trie for storing words and their prefixes?<\/h3><p><strong>Answer:<\/strong><\/p><p>A Trie is a tree-like data structure where each node represents a single character, and paths represent words.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>class TrieNode:<br>\ndef __init__(self):<br>\nself.children = {}<br>\nself.is_end_of_word = False<\/p>\n<p>class Trie:<br>\ndef __init__(self):<br>\nself.root = TrieNode()<\/p>\n<p>def insert(self, word):<br>\nnode = self.root<br>\nfor char in word:<br>\nif char not in node.children:<br>\nnode.children[char] = TrieNode()<br>\nnode = node.children[char]\nnode.is_end_of_word = True<\/p>\n<p>def search(self, word):<br>\nnode = self.root<br>\nfor char in word:<br>\nif char not in node.children:<br>\nreturn False<br>\nnode = node.children[char]\nreturn node.is_end_of_word<\/p>\n<p>def starts_with(self, prefix):<br>\nnode = self.root<br>\nfor char in prefix:<br>\nif char not in node.children:<br>\nreturn False<br>\nnode = node.children[char]\nreturn True<\/p>\n<\/div><\/div><h2>Final Words<\/h2><p>Getting ready for an interview can feel overwhelming, but going through these Data Structures and Algorithms fresher interview questions can help you feel more confident.<\/p><p>With the right preparation, you&rsquo;ll ace your Data Structures and Algorithms interview, but don&rsquo;t forget to practice the fundamentals of data structures, algorithmic problem-solving, and time\/space complexity-related interview questions too.<\/p><hr><h2>Frequently Asked Questions<\/h2><h3>1. What are the most common interview questions for data structures and algorithms?<\/h3><p>Common DSA interview questions focus on fundamental data structures (arrays, linked lists, stacks, queues, trees, graphs) and algorithmic problems like sorting, searching, recursion, dynamic programming, and time complexity analysis.<\/p><h3>2. What are the important DSA topics freshers should focus on for interviews?<\/h3><p>Freshers should focus on arrays, linked lists, stacks, queues, binary trees, binary search trees, heaps, graphs, sorting algorithms, searching algorithms, recursion, dynamic programming, and Big-O time complexity.<\/p><h3>3. How should freshers prepare for data structures and algorithms technical interviews?<\/h3><p>Freshers should practice solving a variety of problems on platforms like LeetCode, Codeforces, or HackerRank, study different data structures and their use cases, and understand how to optimize solutions for time and space complexity.<\/p><h3>4. What strategies can freshers use to solve DSA coding questions during interviews?<\/h3><p>Freshers should first analyze the problem, identify the most suitable data structure, write pseudocode, and choose an algorithm that minimizes time and space complexity. Testing the solution with edge cases is also crucial during interviews.<\/p><h3>5. Should freshers prepare for advanced DSA topics in interviews?<\/h3><p>Yes, freshers should prepare for advanced topics like dynamic programming, graph algorithms (Dijkstra, BFS, DFS), and amortized analysis. While not always asked, these topics can give candidates an edge in technical interviews.<\/p><hr><h2>Explore More DSA Resources<\/h2><ul class=\"explore-more\">\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/best-websites-to-learn-data-structures-and-algorithms\/\">DSA Learning Websites<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/best-websites-to-practice-data-structures-and-algorithms\/\">DSA Practicing Websites<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/best-youtube-channels-to-learn-data-structures-and-algorithms\/\">DSA YouTube Channels<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/dsa-project-ideas-for-beginners\/\">DSA Project Ideas<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/mcq\/data-structures-and-algorithms\/\">Data Structures &amp; Algorithms MCQ<\/a><\/li>\n<\/ul><h2>Explore More Interview Questions<\/h2><ul class=\"explore-more\">\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/python-interview-questions-for-freshers\/\">Python<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/java-interview-questions-for-freshers\/\">Java<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/sql-interview-questions-for-freshers\/\">SQL<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/react-interview-questions-for-freshers\/\">React<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/javascript-interview-questions-for-freshers\/\">JavaScript<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/c-programming-interview-questions-for-freshers\/\">C Programming<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/html-interview-questions-for-freshers\/\">HTML<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/css-interview-questions-for-freshers\/\">CSS<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/angular-interview-questions-for-freshers\/\">Angular<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/cpp-interview-questions-for-freshers\/\">C++<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/spring-boot-interview-questions-for-freshers\/\">Spring Boot<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/node-js-interview-questions-for-freshers\/\">Node JS<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/excel-interview-questions-for-freshers\/\">Excel<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/c-sharp-interview-questions-for-freshers\/\">C#<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/dbms-interview-questions-for-freshers\/\">DBMS<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/php-interview-questions-for-freshers\/\">PHP<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/linux-interview-questions-for-freshers\/\">Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/operating-system-interview-questions-for-freshers\/\">Operating System<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/mysql-interview-questions-for-freshers\/\">MySQL<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/spring-interview-questions-for-freshers\/\">Spring<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/flutter-interview-questions-for-freshers\/\">Flutter<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/mongodb-interview-questions-for-freshers\/\">MongoDB<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/django-interview-questions-for-freshers\/\">Django<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/react-native-interview-questions-for-freshers\/\">React Native<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/jquery-interview-questions-for-freshers\/\">jQuery<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/bootstrap-interview-questions-for-freshers\/\">Bootstrap<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/embedded-c-interview-questions-for-freshers\/\">Embedded C<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Are you preparing for your first Data Structures and Algorithms interview and wondering what questions you might face?Understanding the key Data Structures and Algorithms interview questions for freshers can give you more clarity.With this guide, you&rsquo;ll be well-prepared to tackle these Data Structures and Algorithms interview questions and answers for freshers and make a strong [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":12620,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-12598","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-interview-questions"],"_links":{"self":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/12598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/comments?post=12598"}],"version-history":[{"count":9,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/12598\/revisions"}],"predecessor-version":[{"id":19105,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/12598\/revisions\/19105"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/media\/12620"}],"wp:attachment":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/media?parent=12598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/categories?post=12598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/tags?post=12598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}