Amazon Online Assessment (OA) - Fetch Items To Display

Solution

Explanation

We sort the results by the sortColumn and order. Then, using the pageSize and the pageIndex, we can figure out the number of results on the previous pages and the index of the first result of the target pageIndex page. For example, if pageIndex = 1 and pageSize = 2, there are 1 * 2 = 2 results on page 0, so the index of the first result on page 1 should be 2. Finally, we return all the results on the target page.

1from typing import Dict, List, Tuple
2
3def fetch_results_to_display(sort_column: int, sort_order: int, results_per_page: int, page_index: int, results: Dict[str, Tuple[int, int]]) -> List[str]:
4    ordered = [(name, rel, price) for name, (rel, price) in results.items()]
5    ordered.sort(key=lambda x: x[sort_column], reverse=sort_order == 1) # sort by sort_column and reverse order if needed
6    start_index = results_per_page * page_index # find the start index of the first result on the target page
7    return [name for name, _, _ in ordered[start_index:start_index + results_per_page]] # return only the name of each result on the page
8
9if __name__ == '__main__':
10    sort_column = int(input())
11    sort_order = int(input())
12    results_per_page = int(input())
13    page_index = int(input())
14    results_length = int(input())
15    results = {
16        n: (int(r), int(p))
17        for _ in range(results_length)
18        for n, r, p in [input().split()]
19    }
20    res = fetch_results_to_display(sort_column, sort_order, results_per_page, page_index, results)
21    print(' '.join(res))
22
1import java.util.ArrayList;
2import java.util.HashMap;
3import java.util.List;
4import java.util.Map;
5import java.util.Scanner;
6
7class Solution {
8    public static List<String> fetchResultsToDisplay(int sortColumn, int sortOrder, int pageSize, int pageIndex, Map<String, int[]> results) {
9        ArrayList<String> ordered = new ArrayList<>(results.keySet()); // create a list of result names
10        ordered.sort((a, b) -> {
11            int res;
12            if (sortColumn == 0) { // compare result name alphabetical
13                res = a.compareTo(b);
14            } else {
15                // compare by relevance or price. sortParamter - 1 because subtracting the result name spot
16                res = results.get(a)[sortColumn - 1] - results.get(b)[sortColumn - 1];
17            }
18            return res * (sortOrder == 0 ? 1 : -1); // if reverse order, then * -1
19        });
20        int startIndex = pageSize * pageIndex;
21        return ordered.subList(startIndex, Math.min(startIndex + pageSize, ordered.size()));
22    }
23
24    public static void main(String[] args) {
25        Scanner scanner = new Scanner(System.in);
26        int sortColumn = Integer.parseInt(scanner.nextLine());
27        int sortOrder = Integer.parseInt(scanner.nextLine());
28        int resultsPerPage = Integer.parseInt(scanner.nextLine());
29        int pageIndex = Integer.parseInt(scanner.nextLine());
30        int resultsLength = Integer.parseInt(scanner.nextLine());
31        HashMap<String, int[]> results = new HashMap<>();
32        for (int i = 0; i < resultsLength; i++) {
33            String[] segs = scanner.nextLine().split(" ");
34            results.put(segs[0], new int[] { Integer.parseInt(segs[1]), Integer.parseInt(segs[2]) });
35        }
36        scanner.close();
37        List<String> res = fetchResultsToDisplay(sortColumn, sortOrder, resultsPerPage, pageIndex, results);
38        System.out.println(String.join(" ", res));
39    }
40}
41

Got a question?ย Ask the Teaching Assistantย anything you don't understand.

Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.

โ†
โ†‘TA ๐Ÿ‘จโ€๐Ÿซ