Data Structures & Algorithms Handson solutions cognizant (1)

Sparse Arrays Data Structures and Algorithm Cognizant Handson Solution

In this post we are going to cover the Data Structures & Algorithms Sparse Arrays handson solution asked in Cognizant.

Question of this handson is available here.

Steps to upload answer in GenC Platform:

  1. Click on this link.
  2. Insert the below code in Hacker Rank code editor.
  3. Compile & Execute the Code.
  4. Take screenshot of successful execution.
  5. And upload the screenshot in the platform.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'matchingStrings' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts following parameters:
     *  1. STRING_ARRAY strings
     *  2. STRING_ARRAY queries
     */

    public static List<Integer> matchingStrings(List<String> strings, List<String> queries) {
    // Write your code here
        Map<String, Integer> map = new HashMap<>();
        List<Integer> result = new ArrayList<Integer>();

        for (int i = 0; i < strings.size(); i++) {
            String inputString = strings.get(i);
            if (map.containsKey(inputString)) {
                map.put(inputString, map.get(inputString) + 1);
            } else {
                map.put(inputString, 1);
            }
        }

        for (int i = 0; i < queries.size(); i++) {
            String queryString = queries.get(i);
            if (map.containsKey(queryString)) {
                result.add(i, map.get(queryString));
            }
            else{
                result.add(i,0);
            }
        }

        return result;

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int stringsCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> strings = IntStream.range(0, stringsCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        int queriesCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> queries = IntStream.range(0, queriesCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        List<Integer> res = Result.matchingStrings(strings, queries);

        bufferedWriter.write(
            res.stream()
                .map(Object::toString)
                .collect(joining("\n"))
            + "\n"
        );

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Similar Posts