PubMatic Interview Question

Sort a array, [W B W B B W W B ] === [W W W W B B B B ]

Interview Answers

Anonymous

Jun 26, 2018

I failed, but finally I got the answer: void sort(String[] arr){ int start = 0; int end = arr.length -1; while(start < end){ while(arr[start].equals("W")){ start++; } while(arr[end].equals("B")){ end--; } if(start < end){ arr[start] = "W"; arr[end] = "B"; } start++; end--; } }

7

Anonymous

May 26, 2019

private String[] sort(String[] list){ ArrayList list = new ArrayList(); for(i in list) { if (i.equals("W"){ list.add(0,i) } else{ list.add("B") } } return list.toArray() } }

1

Anonymous

Nov 24, 2019

String string = new WBArray().takeStringInput(); char[] chars = string.toCharArray(); int start =0; int end = chars.length-1; char[] newCharArr = new char[chars.length]; for(int i=0;i

1

Anonymous

Nov 24, 2019

String string = new WBArray().takeStringInput(); char[] chars = string.toCharArray(); int start =0; int end = chars.length-1; char[] newCharArr = new char[chars.length]; for(int i=0;i

2

Anonymous

Oct 31, 2020

import java.util.*; public class SortCharArray { public static void main(String[] args) { int o = 1; int t = 2; char[] a = {'W','W','B','W','B','B','W','B'}; System.out.println("Unsorted Array: "+Arrays.toString(a)); for(int i=0;i

4

Anonymous

Jan 27, 2021

int main() { string str("BBWBBWBWBBW"); int f=0; for(int j=1; j < str.length(); j++){ if(str[j-1]=='B' && str[j]=='W') swap(str[f++], str[j]); else if(str[j-1]=='W') f++; } cout<

1

Anonymous

Nov 24, 2019

for(int i=0;i

1