Algorithm and Data Structures Interview Questions
HackerRank | Practice, Tutorials & Interview Preparation Solutions
This repository consists of solutions to HackerRank and LeetCode in Problem Solving, Data Structures and Algorithms, and Interview Preparation Kit problems with Python, Java, and SQL.
HackerRank Badges
HackerRank Certificates
Python
Code: Green
- Missing Number: Given an array containing n distinct numbers taken from
0, 1, 2, ..., n
find the one that is missing from the array. - Single Number: Given a non-empty array of integers, every element appears twice except for one. Find that single one.
- Longest Common Prefix: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "".
- Counting Elements: Given an integer array arr, count how many elements x there are, such that x + 1 is also in arr. If there are duplicates in arr, count them seperately.
- Add Digits: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
- High Five: Given a list of scores of different students, return the average score of each student's top five scores in the order of each student's id. Each entry
items[i]
hasitems[i][0]
the student's id, anditems[i][1]
the student's score. The average score is calculated using integer division. - Buy and Sell Stock I: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one.
- Buy and Sell Stock II: Say you have an array prices for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
- Prefix Sum: Given an array of integers
nums
, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of all the numbers to the left of the index is equal to the sum of all the numbers to the right of the index. If no such index exists, we shouldreturn -1
. If there are multiple pivot indexes, you should return the left-most pivot index.
Java
Code: Green
- Missing Number: Given an array containing n distinct numbers taken from
0, 1, 2, ..., n
find the one that is missing from the array. - Single Number: Given a non-empty array of integers, every element appears twice except for one. Find that single one.
- Longest Common Prefix: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "".
- Add Digits: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
- High Five: Given a list of scores of different students, return the average score of each student's top five scores in the order of each student's id. Each entry
items[i]
hasitems[i][0]
the student's id, anditems[i][1]
the student's score. The average score is calculated using integer division. - Buy and Sell Stock I: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one.
- Buy and Sell Stock II: Say you have an array prices for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
- Prefix Sum: Given an array of integers
nums
, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of all the numbers to the left of the index is equal to the sum of all the numbers to the right of the index. If no such index exists, we shouldreturn -1
. If there are multiple pivot indexes, you should return the left-most pivot index.