
Walmart


10+ Walmart SDE-2 Interview Questions and Answers
Q1. Maximum Frequency Number Problem Statement
Given an array of integers with numbers in random order, write a program to find and return the number which appears the most frequently in the array.
If multiple elem...read more
Find the number with the maximum frequency in an array of integers.
Create a hashmap to store the frequency of each element in the array.
Iterate through the array to update the frequency count.
Find the element with the maximum frequency and return it.
If multiple elements have the same maximum frequency, return the one with the lowest index.
Q2. Maximum Depth of a Binary Tree Problem Statement
Given the root node of a binary tree with N
nodes, where each node contains integer values, determine the maximum depth of the tree. The depth is defined as the ...read more
Find the maximum depth of a binary tree given its level order traversal.
Traverse the tree level by level and keep track of the depth using a queue data structure.
For each level, increment the depth by 1 until all nodes are processed.
Return the maximum depth encountered during traversal as the final result.
Example: For input [5, 10, 15, 20, -1, 25, 30, -1, 40, -1, -1, -1, -1, 45], the maximum depth is 7.
Q3. Sliding Window Maximum Problem Statement
You are given an array/list of integers with length 'N'. A sliding window of size 'K' moves from the start to the end of the array. For each of the 'N'-'K'+1 possible wi...read more
Sliding window maximum problem where we find maximum element in each window of size K.
Use a deque to store indices of elements in decreasing order within the window.
Pop elements from the deque that are out of the current window.
Add the maximum element to the result for each window.
Q4. Maximum Number With Single Swap
You are given an array of N elements that represent the digits of a number. You can perform one swap operation to exchange the values at any two indices. Your task is to determin...read more
Given an array of digits, find the maximum number that can be achieved by performing at most one swap.
Iterate through the array to find the maximum digit.
If the maximum digit is already at the beginning, find the next maximum digit and swap them.
If the maximum digit is not at the beginning, swap it with the digit at the beginning.
Q5. String Transformation Problem
Given a string (STR
) of length N
, you are tasked to create a new string through the following method:
Select the smallest character from the first K
characters of STR
, remove it fr...read more
Given a string, select smallest character from first K characters, remove it, and append to new string until original string is empty.
Iterate through the string, selecting the smallest character from the first K characters each time.
Remove the selected character from the original string and append it to the new string.
Repeat the process until the original string is empty.
Return the final new string formed after the operations.
Q6. Equilibrium Index Problem Statement
Given an array Arr
consisting of N integers, your task is to find the equilibrium index of the array.
An index is considered as an equilibrium index if the sum of elements of...read more
Find the equilibrium index of an array where sum of elements on left equals sum on right.
Iterate through the array and calculate prefix sum and suffix sum at each index.
Compare prefix sum and suffix sum to find equilibrium index.
Return the left-most equilibrium index found.
Q7. Minimum Cost to Connect Sticks
You are provided with an array, ARR
, of N
positive integers. Each integer represents the length of a stick. The task is to connect all sticks into one by paying a cost to join two...read more
Find the minimum cost to connect all sticks into one by joining two sticks at a time.
Sort the array of stick lengths in non-decreasing order.
Keep adding the two smallest sticks at a time to minimize cost.
Repeat until all sticks are connected into one.
Q8. Convert Sorted Array to BST Problem Statement
Given a sorted array of length N
, your task is to construct a balanced binary search tree (BST) from the array. If multiple balanced BSTs are possible, you can retu...read more
Construct a balanced binary search tree from a sorted array.
Create a recursive function to construct the BST by selecting the middle element as the root.
Recursively construct the left subtree with elements to the left of the middle element and the right subtree with elements to the right.
Ensure that the constructed tree is balanced by maintaining the height difference of left and right subtrees at most 1.
Q9. Minimum Jumps Problem Statement
Bob and his wife are in the famous 'Arcade' mall in the city of Berland. This mall has a unique way of moving between shops using trampolines. Each shop is laid out in a straight...read more
Find the minimum number of jumps Bob needs to make from shop 0 to reach the final shop, or return -1 if impossible.
Use a greedy approach to keep track of the farthest reachable shop from the current shop.
If at any point the current shop is not reachable, return -1.
Update the current farthest reachable shop as you iterate through the array.
Q10. Maximum Length Pair Chain Problem Statement
You are provided with 'N' pairs of integers such that in any given pair (a, b), the first number is always smaller than the second number, i.e., a < b. A pair chain i...read more
Given pairs of integers, find the length of the longest pair chain where each pair follows the given condition.
Sort the pairs based on the second element in ascending order.
Iterate through the sorted pairs and keep track of the maximum chain length.
Update the chain length if the current pair can be added to the chain.
Return the maximum chain length as the result.
Q11. Pair Sum Problem Statement
You are given an integer array 'ARR' of size 'N' and an integer 'S'. Your task is to find and return a list of all pairs of elements where each sum of a pair equals 'S'.
Note:
Each pa...read more
Find pairs of elements in an array that sum up to a given value, sorted in a specific order.
Iterate through the array and for each element, check if the complement (S - current element) exists in a hash set.
If the complement exists, add the pair to the result list.
Sort the result list based on the criteria mentioned in the question.
Return the sorted list of pairs.
Q12. Bipartite Graph Problem Statement
Determine if a given graph is bipartite. A graph is bipartite if its vertices can be divided into two independent sets, 'U' and 'V', such that every edge ('u', 'v') connects a ...read more
Check if a given graph is bipartite by dividing its vertices into two independent sets.
Create two sets 'U' and 'V' to store vertices based on their connections
Use BFS or DFS to traverse the graph and assign vertices to sets
Check if any edge connects vertices within the same set, if yes, the graph is not bipartite
Q13. Top View of a Binary Tree Problem Statement
You are given a Binary Tree of integers. Your task is to determine and return the top view of this binary tree.
The top view of a binary tree consists of all the node...read more
The task is to determine and return the top view of a given Binary Tree of integers.
The top view of a binary tree consists of all the nodes visible when the tree is viewed from the top.
Identify the nodes that appear on the top when looking from above the tree.
Output the top view as a space-separated list of node values from left to right.
Use multiple threads to print numbers from 1 to 100 in an optimized approach.
Divide the range of numbers (1-100) among the threads to avoid overlap.
Use synchronization mechanisms like mutex or semaphore to ensure proper order of printing.
Implement a logic where each thread prints its assigned numbers in sequence.
Consider using a thread pool to manage and optimize thread creation and execution.
Example: Thread 1 prints numbers 1-25, Thread 2 prints numbers 26-50, and so on.
Data abstraction is the process of hiding implementation details and showing only the necessary features of an object.
Data abstraction can be achieved through abstract classes and interfaces in object-oriented programming.
It helps in reducing complexity by providing a simplified view of the data.
By using access specifiers like private, protected, and public, we can control the visibility of data members and methods.
Example: In a car object, we can abstract the details of the ...read more
A system like Pastebin allows users to store and share text snippets online.
Allow users to paste text content into a form
Generate a unique URL for each paste
Set expiration time for pastes to be automatically deleted
Implement syntax highlighting for various programming languages
Provide options for users to set visibility (public/private) of their pastes
DHCP is a network protocol that automatically assigns IP addresses to devices on a network.
DHCP stands for Dynamic Host Configuration Protocol
It allows devices to obtain IP addresses and other network configuration information dynamically
DHCP servers assign IP addresses to devices within a network
DHCP uses a lease mechanism to control the amount of time a device can use an IP address
DHCP reduces the administrative burden of manually assigning IP addresses to devices
TCP/IP is a set of protocols that governs the transmission of data over the internet.
TCP/IP stands for Transmission Control Protocol/Internet Protocol.
It is a suite of communication protocols used to interconnect network devices on the internet.
TCP ensures that data packets are delivered reliably and in order.
IP is responsible for addressing and routing packets to their destination.
Examples of TCP/IP applications include web browsing (HTTP), email (SMTP), and file transfer (F...read more
Design a URL shortener system to generate short URLs for long URLs.
Use a unique identifier for each long URL to generate a short URL.
Store the mapping of short URL to long URL in a database.
Implement a redirection mechanism to redirect short URLs to their corresponding long URLs.

Top SDE-2 Interview Questions from Similar Companies






Reviews
Interviews
Salaries
Users/Month

