This is a verified interview question from Flipkart. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "K means Clustering Code - Flipkart Online Assessment Data Science Role" covers key patterns like Arrays.
"A company wants to group customer records into clusters based on two numerical behavioral features. Since the records are not labeled, use K-Means clustering to create the groups. Generate a synthetic dataset using scikit-learn and group its data points using the number of clusters provided as input. **Dataset Generation:** Generate the dataset using: `from sklearn.datasets import make_blobs` Use the following fixed parameters: * `n_samples=300` * `centers=4` * `n_features=2` * `random_state=0` **Task:** * Write a Python program that performs the following steps: * Generate the synthetic dataset using the specified parameters. * Read two integers from standard input: * `n_clusters` * `random_state` * Initialize a K-Means model using: * `n_clusters` equal to the input value * `random_state` equal to the input value * `n_init=10` * Train the K-Means model using the generated dataset. * Count the number of data points in every cluster. * Sort the cluster sizes in ascending order. * Print each sorted cluster size on a separate line. **Constraints:** 2 <= n_clusters <= 6 0 <= random_state <= 10000 **Input Format:** Two space-separated integers: `<n_clusters><space><random_state>` **Output Format:** Print `n_clusters` lines. Each line must contain one cluster size. The sizes must be printed in ascending order. **Sample Input 1:** ```text 4 0 ``` **Sample Output 1:** ```text 74 74 75 77 ``` **Note:** Cluster label numbers can vary internally. Therefore, print only the sorted cluster sizes. Do not print any extra text or labels."
Join thousands of developers practicing for Flipkart.