"Compress an image represented as a 2D matrix." | UBER QUESTION | BLIND

571
Dec 04, 2023

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.

// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
public class ImageCompression {

    // Function to compress an image represented as a 2D matrix using Run-Length Encoding (RLE)
    public static int[][] compressImage(int[][] image) {
        int rows = image.length;
        int cols = image[0].length;

        // Create a list to store the compressed image
        List<int[]> compressedImageList = new ArrayList<>();

        // Traverse the image matrix
        for (int i = 0; i < rows; i++) {
            List<Integer> compressedRow = new ArrayList<>();
            int count = 1;

            for (int j = 1; j < cols; j++) {
                if (image[i][j] == image[i][j - 1]) {
                    // If the current element is the same as the previous one, increment the count
                    count++;
                } else {
                    // If the current element is different, add the previous element and its count to the compressed row
                    compressedRow.add(image[i][j - 1]);
                    compressedRow.add(count);
                    count = 1; // Reset the count for the new element
                }
            }

            // Add the last element and its count to the compressed row
            compressedRow.add(image[i][cols - 1]);
            compressedRow.add(count);

            // Convert the compressed row to an array and add it to the compressed image list
            int[] compressedRowArray = compressedRow.stream().mapToInt(Integer::intValue).toArray();
            compressedImageList.add(compressedRowArray);
        }

        // Convert the compressed image list to a 2D array
        int[][] compressedImage = new int[compressedImageList.size()][];
        compressedImageList.toArray(compressedImage);

        return compressedImage;
    }
    
    public static int[][] decompressImage(int[][] compressedImage) {
        List<int[]> decompressedImageList = new ArrayList<>();

        for (int[] compressedRow : compressedImage) {
            List<Integer> decompressedRow = new ArrayList<>();

            for (int i = 0; i < compressedRow.length; i += 2) {
                int value = compressedRow[i];
                int count = compressedRow[i + 1];

                // Repeat the value count times in the decompressed row
                for (int j = 0; j < count; j++) {
                    decompressedRow.add(value);
                }
            }

            // Convert the decompressed row to an array and add it to the decompressed image list
            int[] decompressedRowArray = decompressedRow.stream().mapToInt(Integer::intValue).toArray();
            decompressedImageList.add(decompressedRowArray);
        }

        // Convert the decompressed image list to a 2D array
        int[][] decompressedImage = new int[decompressedImageList.size()][];
        decompressedImageList.toArray(decompressedImage);

        return decompressedImage;
    }

    // Function to print a 2D matrix
    public static void printMatrix(int[][] matrix) {
        for (int[] row : matrix) {
            for (int value : row) {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        // Example usage
        int[][] image = {
                {1, 1, 1, 2, 2},
                {3, 3, 4, 4, 4},
                {5, 5, 5, 5, 6}
        };

        System.out.println("Original Image:");
        printMatrix(image);

        int[][] compressedImage = compressImage(image);

        System.out.println("\nCompressed Image:");
        printMatrix(compressedImage);
        
        int[][] decompressedImage = decompressImage(compressedImage);

        System.out.println("\nDecompressed Image:");
        printMatrix(decompressedImage);
    }
}
Comments (0)