Javascript DSA QnA solutions for product based company interview
Here are some JavaScript solutions to common Data Structures and Algorithms (DSA) questions frequently asked in product-based company interviews. These problems focus on fundamental topics like arrays, linked lists, sorting, and searching.
1. Find the Missing Number
Problem: Given an array of n
distinct numbers from the range 1
to n+1
, find the missing number.
Example:
Input: [1, 2, 4, 5, 6]
Output: 3
Solution: We can calculate the expected sum of the array (from 1 to n+1) and subtract the sum of the array elements to find the missing number.
2. Reverse a Linked List
Problem: Given the head of a singly linked list, reverse the list and return its head.
Example:
Input: 1 -> 2 -> 3 -> 4 -> 5
Output: 5 -> 4 -> 3 -> 2 -> 1
Solution:
We can reverse the linked list by updating the next
pointers of the nodes iteratively.
3. Find the Longest Substring Without Repeating Characters
Problem: Given a string, find the length of the longest substring without repeating characters.
Example:
Input: "abcabcbb"
Output: 3
(Longest substring is "abc"
)
Solution: Use a sliding window technique with a hash map to track characters in the current window.
4. Merge Two Sorted Arrays
Problem: Given two sorted arrays, merge them into one sorted array.
Example:
Input: [1, 3, 5]
and [2, 4, 6]
Output: [1, 2, 3, 4, 5, 6]
Solution: We can use two pointers, one for each array, and merge them into a result array.
5. Find the Kth Largest Element in an Array
Problem: Given an array, find the k
th largest element.
Example:
Input: [3, 2, 1, 5, 6, 4]
, k = 2
Output: 5
Solution:
Use a min-heap or Quickselect algorithm to efficiently find the k
th largest element.
Alternatively, using a min-heap for optimized performance:
6. Balanced Parentheses
Problem: Given a string containing just the characters '('
, ')'
, {
, }
, [
, and ]
, determine if the input string is valid. An input string is valid if:
- Open brackets are closed by the correct type of brackets.
- Open brackets are closed in the correct order.
Example:
Input: "()"
Output: true
Solution: Use a stack to keep track of open brackets.
7. Top K Frequent Elements
Problem: Given a non-empty array of integers, return the k
most frequent elements.
Example:
Input: [1,1,1,2,2,3]
, k = 2
Output: [1, 2]
Solution:
Use a hash map to count the frequencies and then use a min-heap to find the top k
elements.
8. Detect a Cycle in a Linked List
Problem: Given a linked list, determine if it has a cycle.
Solution: We can use Floyd’s Cycle-Finding Algorithm (Tortoise and Hare), which uses two pointers moving at different speeds.
9. Binary Search
Problem: Given a sorted array of integers, find the index of a target value.
Example:
Input: [1, 2, 3, 4, 5]
, target = 3
Output: 2
Solution: Implement binary search using two pointers.
Key Takeaways:
- These solutions help demonstrate your understanding of common data structures (arrays, linked lists, hash maps, heaps) and how to apply algorithms (like binary search, sliding window, and sorting) efficiently.
- Always pay attention to the time and space complexities for optimal solutions. For example, binary search has a time complexity of O(log n), while sorting has O(n log n).
- Practice these problems regularly to improve your speed and confidence in tackling technical interviews.
Good luck with your interview preparation!
Comments
Post a Comment