How do you find the largest element in an unsorted array?
Simple meaning
Scan once while keeping a running maximum, initializing it to the first element or negative infinity.
Open the full page for Why, Steps, Example and Key takeaway.
Panel-ready DSA interview set questions for freshers and experienced developers. Practice at Coding Cadre in Faridabad, or Online from Delhi NCR.
Scan once while keeping a running maximum, initializing it to the first element or negative infinity.
Open the full page for Why, Steps, Example and Key takeaway.
Use two pointers at the start and end and swap until they meet.
Open the full page for Why, Steps, Example and Key takeaway.
Track the largest and second-largest while scanning once, updating both when you see a new max and updating second when a value sits between them.
Open the full page for Why, Steps, Example and Key takeaway.
Keep a write index for the next non-zero slot
Open the full page for Why, Steps, Example and Key takeaway.
Walk adjacent pairs and return false on the first decrease.
Open the full page for Why, Steps, Example and Key takeaway.
Reduce k modulo n, reverse the whole array, reverse the first k elements, then reverse the rest.
Open the full page for Why, Steps, Example and Key takeaway.
The expected sum is n*(n+1)/2
Open the full page for Why, Steps, Example and Key takeaway.
Insert every element of both arrays into a hash set, then dump the set into a list.
Open the full page for Why, Steps, Example and Key takeaway.
Two pointers swap from both ends until they meet, which is O(n) time and O(1) extra space.
Open the full page for Why, Steps, Example and Key takeaway.
Compare characters from both ends with two pointers and stop on a mismatch.
Open the full page for Why, Steps, Example and Key takeaway.
If lengths differ they cannot be anagrams.
Open the full page for Why, Steps, Example and Key takeaway.
Count frequencies in a hash map or array, then scan the string again and return the first character with count one.
Open the full page for Why, Steps, Example and Key takeaway.
Trim extra spaces, split on whitespace into words, reverse the word list, then join with single spaces.
Open the full page for Why, Steps, Example and Key takeaway.
Walk the string once and increment a hash map or a 26/128-slot array.
Open the full page for Why, Steps, Example and Key takeaway.
Equal length is required.
Open the full page for Why, Steps, Example and Key takeaway.
While scanning, store each value and its index in a hash map and look up target minus the current number before inserting.
Open the full page for Why, Steps, Example and Key takeaway.
Insert into a hash set and return true as soon as an insert finds the value already present.
Open the full page for Why, Steps, Example and Key takeaway.
Put the smaller array into a hash set, then walk the other and collect values that exist in the set, optionally using a second set for uniqueness.
Open the full page for Why, Steps, Example and Key takeaway.
Boyer-Moore voting keeps a candidate and a count, resetting when the count hits zero
Open the full page for Why, Steps, Example and Key takeaway.
Build a frequency map of the first array, then decrement with the second and reject negatives or leftover keys.
Open the full page for Why, Steps, Example and Key takeaway.