Question d’entretien chez Google

Given a sorted array [0-99] With input: [1, 5, 45, 86] Write a function that prints the empty regions, example Output: “0,2-4,6-44,46-85,87-99”

Réponses aux questions d'entretien

Utilisateur anonyme

26 mai 2015

#include using namespace std; void printRange(int start, int count) { if (count == 1) { cout 1) { cout = 1) cout << ","; index++; count = 0; } } printRange(start, count); cout << endl; return 0; }

Utilisateur anonyme

10 juin 2015

#Python def empty_regions(a): if len(a)>0: if a[0]== 1: print(0) elif a[0] > 1: print('0 -',a[0]-1) #print the rest i = 0 while i < len(a)-1: if a[i]+2 == a[i+1]: print(a[i]+1) elif a[i]+2 < a[i+1]: print(a[i]+1,'-',a[i+1]-1) i += 1 if a[-1] == 98: print(99) elif a[-1]<98: print(a[-1]+1,'- 99') else: print('0-99') ###****************************** a = [1, 5, 45, 86] empty_regions(a)

Utilisateur anonyme

14 mai 2016

private void printRange(int[] array) { int upperLimit = 99; int lowerLimit = 0; for(int num : array){ if(lowerLimit == 0 && num != 0){ System.out.println(0); } else if(num - lowerLimit > 0){ System.out.println((lowerLimit + 1) + "-" + (num - 1)); } lowerLimit = num; } if(lowerLimit != upperLimit){ System.out.println((lowerLimit + 1) + "-" + upperLimit); } }

Utilisateur anonyme

26 mai 2015

public static String printEmpty(int[] input) { StringBuilder sb = new StringBuilder(); if (input[0] != 0) { if(input[0] == 1) { sb.append("0"); } else { sb.append("0-"+(input[0]-1)); } } for (int i = 1; i 1) { sb.append(","); if((input[i] - input[i-1]) == 2) { sb.append(input[i]-1); } else { sb.append((input[i-1] +1) + "-" + (input[i] -1 )); } } } if (input[input.length-1] != 99) { sb.append(","); if (input[input.length-1] == 98) { sb.append("99"); } else { sb.append((input[input.length-1]+1) + "-99"); } } return sb.toString(); }