In this challenge, use the HTTP GET method to retrieve information from a database of Card Transactions records for users. Query https://jsonmock.hackerrank.com/api/transactions/search?txnType=txn where txn is the transaction type of the record passed to the function. This will return all records that have the given transaction type. The query response is paginated and can be further accessed by appending to the query string &page=num where num is the page number.

The query response from the API is a JSON response with the following five fields:

  • page: the current page.
  • per_page: the maximum number of results per page.
  • total: total number of records in the search result.
  • total pages: the total number of pages which must be queried to get all the results.
  • data: an array of JSON objects containing transaction records

The data field in the response contains a list of the transaction records, with each transaction record following the below-described schema:

  • id: the unique ID of the record
  • timestamp: the timestamp when the record was generated (In UTC milliseconds)
  • userld: the user id of the user who performed the transaction
  • userName: the user name of the user who performed the transaction
  • txnType: the transaction type of the transaction, either debit or credit
  • amount: the transaction amount stored as a string with the currency structure and prefixed with the $ sign, e.g. "$2,273.95"
  • location: the object containing the location description of the transaction
    • location.id: the id of the location where the transaction took place
    • location.address: the address of the location where the transaction took place
    • location.city: the city where the transaction took place
    • location.zip Code: the zip code of the location where the transaction took place
  • ip: the IP address of the device which was used to perform the transaction

Given the locationId, locationld, and the provided transaction type, txnType, return a 2d array containing the total amount transacted by each user at the given locationld.

The array will be in the format [[1, 1200], [2, 2333] ] where the item at index 0 in the inner array denotes the id of the user and item at index 1 denotes the total amount transacted (either debit or credit based on input txnType). The items in the outer array should be sorted by the ids of the user. Note that the search is not case sensitive.

Function Description

getTransactions has the following parameter(s):
int locationld: the id of the location by which record will be fetched, to be matched with the property location.id
string txnType: the transaction type to filter the records on
Returns:
[int[]]: a 2d array containing the total amount transacted by each user at the given location id. If no records are found matching the filter criteria, it should return [[-1, -1]].

Note: The total amount should be rounded off to 2 places after the decimal. It can also be returned as a string.

Input Format For Custom Testing

The first line contains an integer, locationld, the id of the location of interest.

The second line contains a string, txn Type, the transaction type to filter for

Sample Case 0

Sample Input For Custom Testing

STDIN Function
----- --------
1 -> locationId = 1
debit -> txType = 'debit'

Sample Output

1 13200.08
2 16745.72
3 18859.77
4 10360.32

Given txn Type = debit the query is https://jsonmock hackerrank.com/api/transactions/search?txnType=debit and the response includes:

[
{ id: 1,
userId: 1,
userName: 'John Oliver',
timestamp: 1549536882071,
txnType: 'debit',
amount: '$1,670.57',
location: { id: 7,
address: '770, Deepends, Stockton Street',
city: 'Ripley',
zipCode: 44139
},
ip: '212.215.115.165'
}...
]

Given the response from the API for txn Type = 'debit', filter the records. After filtering the data to find all the records which belong to location.id = 1, find the total amount spent by each user. The final values for user 1 is 11590.28, for user 2 is 17410.38 and so on. Finally create the 2d array containing the answer [[1, 13200.08], [2, 16745.72], [3, 18859.77], [4, 10360.32]] which is returned.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.