GS DS&Algo Problems & Test Cases
GS DS&Algo Problems & Test Cases
Contents
Mathematics Problems:..............................................................................................................................3
Add Fraction............................................................................................................................................3
Dot Product.............................................................................................................................................3
Is Power of 10..........................................................................................................................................3
Power of Expo ( Math.pow).....................................................................................................................4
Prime Factorization..................................................................................................................................4
Square root..............................................................................................................................................4
Decimal Conversion.................................................................................................................................4
String & Pattern Problems:.........................................................................................................................5
Dist. Between Strings...............................................................................................................................5
Longest Word..........................................................................................................................................5
Apache Log Pattern..................................................................................................................................6
First NonRepeating Character.................................................................................................................7
Group Anagrams......................................................................................................................................7
Longest Uniform Substring......................................................................................................................7
Run Length Encoding...............................................................................................................................8
Pangram..................................................................................................................................................8
Reverse String..........................................................................................................................................8
Reverse String Bug...................................................................................................................................9
Numbers/Numeric Problems:.....................................................................................................................9
Smallest Number.....................................................................................................................................9
Second Smallest.......................................................................................................................................9
Data Structure Implementation:..............................................................................................................10
Deque....................................................................................................................................................10
HashMap...............................................................................................................................................10
Tree...........................................................................................................................................................11
Search Tree............................................................................................................................................11
Largest Tree...........................................................................................................................................13
Arrays:.......................................................................................................................................................14
Median Two Sorted Arrays....................................................................................................................14
SubArray Exceeding Sum.......................................................................................................................15
1
GS Incubation coding FAQ
Dynamic Programming:............................................................................................................................15
Student Election Program......................................................................................................................15
Walking Robot.......................................................................................................................................16
Optimal Path..........................................................................................................................................17
Staircase................................................................................................................................................17
Train Map..............................................................................................................................................18
Miscellaneous:..........................................................................................................................................19
Count Length Of Cycle...........................................................................................................................19
Magic Potion..........................................................................................................................................20
Pascals Triangle.....................................................................................................................................20
Unique Tuples........................................................................................................................................21
Best Average Grade...............................................................................................................................21
Snowpack..............................................................................................................................................22
Mathematics Problems:
Add Fraction
Problem Statement-
2
GS Incubation coding FAQ
Signature:
Test Cases:
INPUT:
result = [7,6]
Dot Product
Problem Statement-
Signature:
Result =[8]
Is Power of 10
Problem Statement-
Signature:
3
GS Incubation coding FAQ
Test Cases:
INPUT:
Input1: 3
Output1: false
Input1: 10
Output1: true
Problem Statement-
Given base and integer exponent, compute value of base raised to the power of exponent.
Signature:
}k
Test Cases:
Input: 2.0
Output: 16.0
Prime Factorization
Problem Statement:
Signature:
4
GS Incubation coding FAQ
Output:[2,3]
Square root
Problem Statement-
Signature:
Test Cases:
Input1: 4
Ouput1:2
Input2:2
Output2:1.41421
Decimal Conversion
Problem Statement:
Implement the method that provided numerator and denominator will return a string
representing fraction's decimal form.
Some fractions in decimal form have cyclic decimal points.
Test Cases:
vulgarToDecimal(1l, 2l).equals("0.5");
vulgarToDecimal(1l, 3l).equals("0.(3)");
vulgarToDecimal(1l, 30l).equals("0.0(3)");
vulgarToDecimal(1l, 75l).equals("0.01(3)");
vulgarToDecimal(4l, 7l).equals("0.(571428)");
5
GS Incubation coding FAQ
Given two words returns the shortest distance between their two midpoints in number of
characters, words can appear multiple times in any order and should be case insensitive.
Signature :
public static double shortestDistance(String document, String word1, String word2)
{
Test Cases:
String Document – “In publishing and graphic design, lorem ipsum is a filler text commonly used to
demonstrate the graphic elements”.
Longest Word --
Problem Statement-
Test Cases-
Dictionary dict = new Dictionary(new String[]{"to", "toe", "toes", "doe", "dog", "god", "dogs",
"book", "banana"});
Input- toe
Output- toe
Input –oetdg
6
GS Incubation coding FAQ
Given an Apache log file, return IP address(es) which accesses the site most often.
our log is in this format (Common Log Format). One entry per line.
10.0.0.1 - frank [10/Dec/2000:12:34:56 -0500] "GET /a.gif HTTP/1.0" 200 234
Log file entries are passsed as an array.
NOTE: In case of tie, this returns a comma-separated list of the IPaddresses. Tie is not
mentioned explicitly in the exercise on purpose.
Signature:
String findTopIpaddress(String[] lines){
}
Test Cases:
Input:
String lines[] = new String[]{
"10.0.0.1 - frank [10/Dec/2000:12:34:56 -0500] \"GET /a.gif HTTP/1.0\" 200 234",
"10.0.0.1 - frank [10/Dec/2000:12:34:57 -0500] \"GET /b.gif HTTP/1.0\" 200 234",
"10.0.0.2 - nancy [10/Dec/2000:12:34:58 -0500] \"GET /c.gif HTTP/1.0\" 200 234"};
Output :10.0.0.1
Input:
String lines[] = new String[]{
"10.0.0.1 - frank [10/Dec/2000:12:34:56 -0500] \"GET /a.gif HTTP/1.0\" 200 234",
"10.0.0.1 - frank [10/Dec/2000:12:34:57 -0500] \"GET /b.gif HTTP/1.0\" 200 234",
"10.0.0.2 - nancy [10/Dec/2000:12:34:58 -0500] \"GET /c.gif HTTP/1.0\" 200 234",
"10.0.0.2 - nancy [10/Dec/2000:12:34:59 -0500] \"GET /c.gif HTTP/1.0\" 200 234",
"10.0.0.3 - logan [10/Dec/2000:12:34:59 -0500] \"GET /d.gif HTTP/1.0\" 200 234",};
Output - 10.0.0.1,10.0.0.2
Finds the first character that does not repeat anywhere in the input string
If all characters are repeated, return 0
Given “apple”, the answer is “a”
Given “racecars”, the answer is “e"
7
GS Incubation coding FAQ
Signature :
Test Cases-
Input: apple
Output: a
Input – xxyyzz
Output - 0
Group Anagrams
Problem Statement:
Test Cases –
This method should return an integer array with two elements that correctly identifies the
location of the longest uniform substring within the input string. The first element of the
array should be the starting index of the longest
substring and the second element should be the length.
input: “abbbccda" the longest uniform substring is “bbb” (which starts at index 1 and is 3 characters
long.
Signature :
8
GS Incubation coding FAQ
int[] longestUniformSubstring(String input) {
-Test Cases –
Input :aabbbbbCdAA
Output – [hi]
Signature :
Test Case:
Input :aaabbbaad
Output: a3b3a2d1
Pangram
Problem Statement-
The sentence “The quick brown fox jumps over the lazy dog" contains
every single letter in the alphabet. Such sentences are called pangrams.
write a function findMissingLetters, which takes a String “sentence,
and returns all the letters it is missing (which prevent it from
being a pangram). You should ignore the case of the letters in sentence,
and your return should be all lower case letters, in alphabetical order.
you should also ignore all non US-ASCII characters.
Signature:
9
GS Incubation coding FAQ
Test Cases:
Output:bfgjkvz
Reverse String
Problem Statement :
Test Cases:
Input :abcd
Output: dcba
Test Cases:
10
GS Incubation coding FAQ
Input :abcd
Output: dcba
Numbers/Numeric Problems:
Smallest Number
Problem Statement-
Signature:
Test Cases:
Input: [3, 4, 5, 6, 1, 2]
Output: 1
Input: [2, 1]
Output:1
Second Smallest
Problem Statement-
Returns second smallest element in array x. If x has fewer than 2 elements returns 0.
Signature:
int secondSmallest(int[] x)
11
GS Incubation coding FAQ
Test Cases:
Input:[-1, 0, 1, -2, 2]
Output: -1
Input:[0, 1]
Output: 1
Signature:
12
GS Incubation coding FAQ
small set of data, and scales linearly (at worst) for larger
sets of key-value pairs.
Each unique key is associated with one single value.
Signature :
Tree
Search Tree
Problem Statement:
13
GS Incubation coding FAQ
private Node root;
public BST() {
this.root = new Node();
}
public List<Integer>inOrderTraversal() {
final ArrayList<Integer> acc = new ArrayList<>();
inOrderTraversal(root, acc);
return acc;
}
14
GS Incubation coding FAQ
} else {
System.out.println("Test passed");
}
}
Largest Tree
Problem statement:
Given a forest ( one or more disconnected trees ), find the root of largest tree
and return its Id. If there are multiple such roots, return the smallest Id of them.
15
GS Incubation coding FAQ
Example:
Input:
{{1->2}, {3 -> 4} }
Expected output: 2
Explanation: There are two trees one having root of Id 2 and another having root of Id 4.
Both trees have size 2. The smaller number of 2 and 4 is 2. Hence the answer is 2.
Signature :
Arrays:
Median Two Sorted Arrays
Problem Statement:
16
GS Incubation coding FAQ
Find the median of the two sorted arrays
Signature:
Test Cases:
Input:
Arr1= [1,3];
Arr2 = [2,4];
Output:
2.5
Input:
Arr1 = [1,3]
Arr2= [2]
Output:2.0
Your task is ultimately to implement a function that takes in an array and a integer.
You want to return the *LENGTH* of the shortest subarray whose sum is at least the integer,
and -1 if no such sum exists.
Signature:
Test Cases:
Input:[1,2,3,4,] , k=6
Output :2
17
GS Incubation coding FAQ
Input:[1,2,3,4,] , k=-1
Output :12
Dynamic Programming:
Student Election Program
Problem Statement:
A group of students are sitting in a circle. The teacher is electing a new class president.
The teacher does this by singing a song while walking around the circle. After the song is
finished the student at which the teacher stopped is removed from the circle.
Starting at the student next to the one that was just removed, the teacher resumes singing
and walking around the circle.
after the teacher is done singing, the next student is removed. The teacher repeats this until
only one student is left.
A song of length k will result in the teacher walking past k students on each round. The
students are numbered 1 to n. The teacher starts at student 1.
For example, suppose the song length is two (k=2). And there are four students to start with
(1,2,3,4). The firststudent to go would be “2°, after that “4°, and after that ~3>. Student ~1°
would be thenext president in this example.
Signature:
Test Cases:
Input :1, 1
Output :1
18
GS Incubation coding FAQ
Input : 2, 2
Output :1
Input : 4,2
Output :1
Walking Robot
Problem Statement:
Signature:
UUUDLR
x=0
y=0;
case 'R':x++
Test Cases:
19
GS Incubation coding FAQ
Input: “”(Blank)
Output: [0,0]
Input: “L”
Output: [-1,0]
Input: “UUU”
Output: [0,3]
Input: “ULDR”
Output: [0,0]
Optimal Path
Problem Statement:
You are an avid rock collector who lives in southern California. Some rare
and desirable rocks just became available in New York, so you are planning
a cross-country road trip. There are several other rare rocks that you could
pick up along the way.
You have been given a grid filled with numbers, representing the number of
rare rocks available in various cities across the country. Your objective
is to find the optimal path from So_Cal to New_York that would allow you to
accumulate the most rocks along the way.
Note: You can only travel either north (up) or east (right).
2) Consider adding some additional tests in doTestsPass().
3) Implement optimalPath() correctly.
4) Here is an example:
^
{{0, 0, 0, 0, 5}, New_York (finish) N
{0, 1, 1, 1, 0}, < W E >
So_Cal (start) {2, 0, 0, 0, 0}} S
v
The total for this example would be 10 (2 + 0 + 1 + 1 + 1 + 0 + 5).
Signature:
20
GS Incubation coding FAQ
Test Cases:
Input :
{{0, 0, 0, 0, 5},
{0, 1, 1, 1, 0},
{2, 0, 0, 0, 0}};
Output: 10
Staircase
Problem Statement:
Signature :
Test Cases:
Input : 3
Output: 4
Input : 1
Output: 1
Input : 2
Output: 2
Input : 10
21
GS Incubation coding FAQ
Output: 274
Input : -5
Output: 0
Train Map
Problem Statement:
/*
* Visual representation of the Train map used
*
* King's Cross St Pancras --- Angel ---- Old Street
* | \ |
* | \ |
* | \ |
* Russell Square Farringdon --- Barbican --- Moorgate
* | /
* | /
* | /
* Holborn --- Chancery Lane --- St Paul’s --- Bank
*/
public TrainMap() {
this.stations = new HashMap<>();
}
22
GS Incubation coding FAQ
}
fromStation.addNeighbour(toStation);
toStation.addNeighbour(fromStation);
return this;
}
Miscellaneous:
Count Length Of Cycle
Problem Statement:
Examples:
Signature:
Input :
arr: [1,2,0]
startIndex: 0
Output :2
23
GS Incubation coding FAQ
Magic Potion --
Brute Force
Problem Statement:
ABABCABABCD
AB*C*D
AB*CA
ABABC*D
Signature:
Test Case:
Input:ABCDABCE
Output:8
Input:ABCABCE
Output: 5
Pascals Triangle
Problem Statement:
The first and last numbers of each row in the triangle are 1
Each number in the triangle is the sum of the two numbers above it.
24
GS Incubation coding FAQ
Dp[][]
{
}
Example:
1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
Col,
Signature:
Test Cases:
Input : 0,0
Output:1
Input :1,2
Output:2
Input :4,8
Output:70
25
GS Incubation coding FAQ
Unique Tuples
Problem Statement:
Given a string and size of the tuples, extracts all unique tuples(substrings) of the given size.
Signature:
Test Cases:
Input :abbccde, 2
Output :
["ab"
"bb",
"bc",
"cc",
"cd",
"de"]
Given a list of student test scores, find the best average grade.
Each student may have more than one test score in the list.
If you end up with an average grade that is not an integer, you should
use a floor function to return the largest integer less than or equal to the average.
Return 0 for an empty input.
Example:
26
GS Incubation coding FAQ
Input:
[ [ "Bobby", "87" ],
[ "Charles", "100" ],
[ "Eric", "64" ],
[ "Charles", "22" ] ].
Expected output: 87
Explanation: The average scores are 87, 61, and 64 for Bobby, Charles, and Eric,
respectively. 87 is the highest.
Signatue:
Test Cases:
Input :
{{"Sarah", "91"},
{"Goldie", "92"},
{"Elaine", "93"},
{"Elaine", "95"},
{"Goldie", "94"},
{"Sarah", "93"}}
Output: 94
Snowpack
Problem Statement:
Given an array of non-negative integers representing the elevations
from the vertical cross section of a range of hills, determine how
many units of snow could be captured between the hills.
27
GS Incubation coding FAQ
___|___|___|___|___|___|___|___|___|___|___|___
{ 0, 1, 3, 0, 1, 2, 0, 4, 2, 0, 3, 0 }
___
___ | | ___
| | * * _*_ * | |_*_ * | |
* ___| | * _*_| | * | | | * | |
___|___|___|_*_|___|___|_*_|___|___|_*_|___|___
{ 0, 1, 3, 0, 1, 2, 0, 4, 2, 0, 3, 0 }
Input :{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
Output:10
28