You are on page 1of 16

Contents

1. Account Transaction
2. Data Finder
3. Fizz Buzz
4. Joined Logger
5. Note Store
6. Staff List
7. Step Counter
8. Strip Property
9. GitHub Solutions
1. Account Transactions
Create a solutions to maintain a bank account’s balance.

Implement the Account class that manages an account’s balance. The class has the following constructor
and methods:

1. The constructor Account(balance) where parameter balance denotes the initial balance of the
account.
2. The method debit(amount) debits the amount from the account and returns true. If insufficient
balance, do not debit and returns false.
3. The method getBalance() returns the current balance.
4. The method credit(amount) credits the amount to the account.

Note: You may assume that the amount parameter passed is always positive or 0

The locked stub code validates the correctness of the Account class implementation by performing the
following operations:

 Debit amount: This operation debits the amount. If return value is false, it prints ‘insufficient
balance’. Otherwise it prints ‘<amount> debited’
 Credit amount: This operation credits the amount and prints ‘<amount> credited’
 GetBalance: This operation gets the current balance and prints ‘Current balance is <balance>’

Input Format for Custom Testing

The first line contains an integer that denotes the initial balance when the Account object is created. The
second line contains an integer n, the number of operations to be performed. Each line i of the n
subsequent lines (where 0 ≤ i ≤ n) contains one of the three operations listed above and their
parameters, if any.

Sample Case 0

Sample Input

1000 -> initial balance

count n = 3 -> operations

Debit 1000 -> first operation

Credit 1000

GetBalance

Sample Output

Account created with initial balance of 1000

1000 debited

1000 credited
Current balance is 1000

Explanation

An account object is created with an initial balance of 1000. The debit operation is performed
debit(1000) which returns true, followed by a credit operation credit(1000). Finally, current balance is
called.

Sample Case 1

Sample Input

10000

Debit 100000

Credit 1000

GetBalance

Sample Output

Account created with initial balance of 10000

Insufficient Balance

1000 credited

Current balance is 11000

[Code Snippet in the Next Page]


2. Data Finder
Implement the function dataFinder such that:

 It takes a single argument data, an array of integer


 It returns a new function that is called find
o find takes 3 arguments: minRange (Integer), maxRange (Integer), and Value (Integer). It
performs the following:
o it searches for the value in the data array in the inclusive range [minRange – maxRange]
using 0 based indexing. If value is found in the given range, it returns true. Otherwise it
returns false
o If minRange or maxRange is beyond an end of the array, throws an Error object with the
message of ‘Invalid range’

For example calling dataFinder([1,6,3,0,2,15,10]) must return a function find such that calling
find(2,4,10) returns false. It searches for 10 in the inclusive range 2 through 4, the subarray [3,0,2]. It is
not found in the given range as it lies at index 6. So the function returns false.

The implementation will be tested by a provided code stub using several input files with parameters.
The dataFinder function will be called with the data parameter, and the returned function will be called
with the minRange, maxRange and value parameters. The result will be printed to the standard output
by the provided code.
Constraints:

 The maximum length of the passed array is 10.

Input format for Custom Testing

The first line contains space-separated integers, the integers in the data array. The second line contains
space-separated integers, the minRange, maxRange and value respectively.

Sample Case 0

Sample Input

data = [15,1,10,5,4,20]

minRange = 1, maxRange = 4, value = 4

Sample output

true

Explanation

The call dataFinder([15,1,10,5,4,20]) returns a function which is called with find(1,4,4). It returns true, 5
is in the range (1-4) at index 4

Sample Case 1

Sample Input

10 1 0 13 4 15

1 10 13

Sample Output

Error: Invalid Range

Code Snippet:
3. Fizz Buzz
Given a number n, for each integer i in the range from 1 to n inclusive, print one value per line as
follows:

 If i is a multiple of both 3 and 5 print FizzBuzz


 If i is a multiple of 3 (but not 5) print Fizz
 If i is a multiple of 5 (but not 3) print Buzz
 If i is not a multiple of 3 or 5, print the value of i

Function Description

FizzBuzz has the following parameter(s):

int n: upper limit of values to test (inclusive)

Returns: None

Prints: The function must print the appropriate response for each value, i in the set {1,2, … n} in
ascending order, each on a separate line

Constrains

0 < n < 2 x 105

Sample Case

Sample Input

n = 15
Sample Output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Explanation
The number 3, 6, 9, and 12 are multiple of 3 (but not 5), so print Fizz on those lines. The number 5 and
10 are multiple of 5 (but not 3), so print Fizz on those lines. The number 5 and 10 are multiples of 5 (but
not 3), so print Buzz on those lines. The number 15 is a multiple of both 3 and 5, so print FizzBuzz on that
line. None of the other values is a multiple of either 3 or 5, so print the value of i on those lines.

4. Joined Logger
In this challenge, each message object has two properties:

 Property level having an integer value


 Property text having a string value

For example:

msg = { level: 2, text: “foo” }

there is an implementation of a simple logger function provided that:

 Takes a message object as an argument


 Writes the text of the message to the defined output

Implement a function joinedLogger that:

 Takes two arguments: integer level and string separator


 Returns a function, f such that f takes a variable number of message objects
o The function f uses the logger to write joined text values of messages that have a level
value greater than or equal to the level parameter.
o The text values must be joined by the separator string parameter, in the order they are
passed to the function.

For example, let’s say there are 3 defined messages:

msg1 = { level:10, text: “foo”}

msg2 = { level: 20, text: “bar”}

msg3 = { level: 30, text: “baz”}

Calling joinedLogger(15, ‘;’) must return a function f, such that calling f(msg1, msg2, msg3) causes the
logger to write the string “bar;baz” to the defined output. The level passed to the joinedLogger is 15 and
the separator is ‘;’ Only msg2 and msg3 have a level greater than or equal to 15, so the text of those
messages “bar” and “baz” is joined with the ‘;’ separator and written to the defined output by the
logger.

Sample case 0

Sample Input

21 ; -> level = 21, separator = ‘;’


4 -> n = 4

40 foo -> msg0.level = 40, msg0.text = ‘foo’

90 bar -> msg1.level = 90, msg1.text = ‘bar’

20 baz -> msg2.level = 20, msg2.text = ‘baz’

21 bax -> msg3.level = 21, msg3.text = ‘bax’

Sample Output

foo;baz;bax

Explanation

The first, the second and fourth message have levels greater than or equal to 21. Their values, joined
with ‘;’ are written by the logger to the defined output.

Sample Case 1

Input

10 -> level = 10, separator = ‘-‘

2 -> n = 2

20 item1 -> msg0.level = 20, msg0.text = ‘item1’

17 item2 -> msg1.level = 17, msg1.text = ‘item2’

Output

item1-item2

5. Notes Store
In this challenge, the task is to create a class NotesStore. The class will manage a collection of notes,
with each note having a state and a name. Valid states for notes are: ‘completed’, ‘active’ and ‘others’.
All other states are invalid.

The class must have following methods:

1. addNotes(state, name): adds a note with the given name and state to the collection. In addition to
that:

 If the passed name is empty, then it throws an Error with the message ‘Name cannot be empty’.
 If the passed name is non-empty but the given state is not a valid state for a note, then it
throws an Error with the message ‘Invalid state {state}’.

2. getNotes(state): returns an array of names of notes with the given state added so far. The names are
returned in the order the corresponding notes were added. In addition to that:
 If the given state is not a valid note state, then it throws an Error with the message ‘invalid state
{state}’
 If no note is found in this state, it returns an empty array.

Note: the state names are case sensitive

Your implementation of the function will be tested by a stubbed code on several input files. Each input
file contains parameters for the function call. The function will be called with those parameters, and the
result of their executions will be printed to the standard output by the provided code. The stubbed code
joins the strings returned by the getNotes function with a comma and prints to the standard output. If
getNotes returns an empty array, the stubbed code prints ‘No Notes’. The stubbed code also prints
messages of all the thrown errors.

Input Format For Custom Testing

In the first line, there is an integer n, denoting the number of operations to be performed. Each line i of
the n subsequent lines (where 0 ≤ i < n) contains space separated string such that the first of them is a
function name, and the remaining ones, if any, are parameters for that function.

Sample Case 0

addNote active DrinkTea

addNote active DrinkCoffee

addNote completed Study

getNotes active

getNotes completed

getNotes foo

Sample Output

DrinkTea, DrinkCoffee

Study

Error: Invalid state foo

Explanation

For all 3 addNote operation, the addNote function is called with a state and a name. Then, the getNotes
function is called for ‘active’ state and ‘completed’ state respectively, and the result is printed. Then
getNotes function is called for ‘foo’ state, which throws an error since this state is invalid, and the error
is printed.
6. Staff List
The task is to create a class StaffList. The class will manage a collection of staff members, where each
member is uniquely identify by a name. The class must have the following methods:

1. add(name, age):

 Parameters string name and integer age are passed to the function
 If age is greater than 20, it adds the member with the given name to the collection
 Else if age is less than or equal to 20, it throws an Error with the message ‘Staff member age
must be greater than 20’.
 It is guaranteed that at any time, if a member is in the collection, then no other member with
the same name will be added to the collection

2. remove(name):

 If the member with the given name is in the collection, it removes the member from the
collection and returns true
 Else if the member with the given name is not in the collection, it does nothing and returns
false.

3. getSize():
 Returns the number of members in the collection

Your implementation of the class will be tested by a stubbed code on several input files. Each input file
contains parameters for the function calls. The functions will be called with those parameters, and the
result of those executions will be printed to the standard output by the provided code. The stubbed
code prints values returned by the remove(name) and getSize() functions, and it also print messages of
all the cached errors.

Sample Case 0:

Sample Input:

add John 25

add Robin 23

getSize

remove Robin

getSize

Sample Output:

True

Explanation: There are 2 staff members, ‘John’ and ‘Robin’, who are added by calling the add function
twice. getSize is then called and returns the numbers of members in the collection, which is 2. Then the
staff member ‘Robin’ is removed from the list by calling the remove function, and since the given name
is in the collection, the return value true is printed. Finally, getSize is called, which prints the size of the
collect, which is now 1 because ‘Robin’ was removed

Sample Case 1:

Sample Input:

add John 20

add Robin 10

getSize

remove Robin

getSize

Sample Output:
Error: Staff member age must be greater than 20

Error: Staff member age must be greater than 20

false

7. Step Counter
In this challenge, you are provided with the implementation of a simple counter object:
const counter = (function counter() {
let value = 0;
return {
getValue: function(){
return value;
},
changeBy: function(k) {
value += k;
},
}
})();

Your task is to implement a function stepCounter that:

 Takes a single parameter k


 Returns a new object, representing a step counter with the initial value of 0 and with three
methods:
o increment(): increments the current value by k
o decrement(): decrement the current value by k
o getValue(): return the current value

Your implementation must encapsulate the provided counter object and use it for its implementation.
The object returned by stepCounter must not have a changeBy property.

Your implementation of the function will be tested by a provided code stub on several input files. Each
input file contains a parameter for stepCounter, followed by several values denoting the operations to
perform on the object return by stepCounter. The results of performing the operations will be printed to
the standard output by the provided code.

Input format for custom testing

In the first line, there is a single integer k denoting the parameter for the stepCounter function. In the
second line, there is integer n, denoting the number of operations to perform. Next n lines follow. Each
of them contains a single character, either + or – or ? denoting respectively calling increment(),
decrement() and getValue() methods on the object returned by stepCounter.
Sample Case 0

1 -> parameter k=1

4 -> number of operations n=4

+ -> increment()

? -> getValue()

- -> decrement()

? -> getValue()

Sample Output

Explanation

In this test, the k parameter for stepCounter is 1, so each increment must increment the value by 1 and
each decrement must decrement the by 1. Initially, the counter has the value 0. There are 4 operations
to be performed. The first of them increments the counter, so it has the value 1 now. The second prints
the current value of the counter. The third decrements the counter so it has the value 0 now. The fourth
prints the current value of the counter

Sample case 1

2 -> parameter k=2

5 -> number of operations, n=5

- -> decrement()

? -> getValue()

+ -> increment()

+ -> increment()

? -> getValue()

Sample output

-2

2
8. Strip Property
In this challenge, the task is to implement a function stripProperty that:

 Takes 2 arguments: an object literal obj and a string prop


 Returns a new object literal with the same properties and their values as obj excluding the
property named prop, if it exists.

Sample Input

STDIN Function

3 -> number of properties, n = 3

foo 2 -> obj.foo = 2

bar -> obj.bar = 3

baz -> obj.baz = 3

foo -> property to be stripped = foo

Sample Output

bar 3

baz 3

Explanation
In this test, obj has 3 properties: foo, bar and baz. The property to strip is foo, so the return obj literal
contains properties bar and baz from the obj with the same values, but it doesn’t contain the property
foo.

GitHub Solutions
Follow:- Sazzad-Saju

Repo:- HackerRank-Solutions

(END)

You might also like