"Compress an image represented as a 2D matrix." | UBER QUESTION | BLIND
Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2023-12-04Added By Anonymous
Source: Link
Company: Uber
Original Post Content:
Found a question on Blind where someone was asked to "Compress an image represented as a 2D matrix." and the candidate got confused and was not able to answer below is one of the answer I can think of
Since it is a very generic question, interviewer might just want to understand how you can tackle and understand the problem and approach it.
Even though it is very basic algorithm but it can be a possible answer to a vague question.
1// Online Java Compiler
2// Use this editor to write, compile and run your Java code online
3import java.util.*;
4public class ImageCompression {
5
6// Function to compress an image represented as a 2D matrix using Run-Length Encoding (RLE)
7public static int[][] compressImage(int[][] image) {
8int rows = image.length;
9int cols = image[0].length;
10
11// Create a list to store the compressed image
12List<int[]> compressedImageList = new ArrayList<>();
13
14// Traverse the image matrix
15for (int i = 0; i < rows; i++) {
16List<Integer> compressedRow = new ArrayList<>();
17int count = 1;
18
19for (int j = 1; j < cols; j++) {
20if (image[i][j] == image[i][j - 1]) {
21// If the current element is the same as the previous one, increment the count
22count++;
23} else {
24// If the current element is different, add the previous element and its count to the compressed row
25compressedRow.add(image[i][j - 1]);
26compressedRow.add(count);
27count = 1; // Reset the count for the new element
28}
29}
30
31// Add the last element and its count to the compressed row
32compressedRow.add(image[i][cols - 1]);
33compressedRow.add(count);
34
35// Convert the compressed row to an array and add it to the compressed image list
36int[] compressedRowArray = compressedRow.stream().mapToInt(Integer::intValue).toArray();
37compressedImageList.add(compressedRowArray);
38}
39
40// Convert the compressed image list to a 2D array
41int[][] compressedImage = new int[compressedImageList.size()][];
42compressedImageList.toArray(compressedImage);
43
44return compressedImage;
45}
46
47public static int[][] decompressImage(int[][] compressedImage) {
48List<int[]> decompressedImageList = new ArrayList<>();
49
50for (int[] compressedRow : compressedImage) {
51List<Integer> decompressedRow = new ArrayList<>();
52
53for (int i = 0; i < compressedRow.length; i += 2) {
54int value = compressedRow[i];
55int count = compressedRow[i + 1];
56
57// Repeat the value count times in the decompressed row
58for (int j = 0; j < count; j++) {
59decompressedRow.add(value);
60}
61}
62
63// Convert the decompressed row to an array and add it to the decompressed image list
64int[] decompressedRowArray = decompressedRow.stream().mapToInt(Integer::intValue).toArray();
65decompressedImageList.add(decompressedRowArray);
66}
67
68// Convert the decompressed image list to a 2D array
69int[][] decompressedImage = new int[decompressedImageList.size()][];
70decompressedImageList.toArray(decompressedImage);
71
72return decompressedImage;
73}
74
75// Function to print a 2D matrix
76public static void printMatrix(int[][] matrix) {
77for (int[] row : matrix) {
78for (int value : row) {
79System.out.print(value + " ");
80}
81System.out.println();
82}
83}
84
85public static void main(String[] args) {
86// Example usage
87int[][] image = {
88{1, 1, 1, 2, 2},
89{3, 3, 4, 4, 4},
90{5, 5, 5, 5, 6}
91};
92
93System.out.println("Original Image:");
94printMatrix(image);
95
96int[][] compressedImage = compressImage(image);
97
98System.out.println("\nCompressed Image:");
99printMatrix(compressedImage);
100
101int[][] decompressedImage = decompressImage(compressedImage);
102
103System.out.println("\nDecompressed Image:");
104printMatrix(decompressedImage);
105}
106}