Google Interview Question
Software DevelopersCountry: United States
binary search recursively:
def maxFontSize(s,W,H,left,right):
if right<left:
return right
mid=(left+right)//2
h=getCharacterHeight(mid)
w=0
for c in s:
w+=getCharacterWidth(c,mid)
if h==H and w==W:
return mid
if w>W or h>H:
return maxFontSize(s,W,H,left,mid-1)
if w<W or h<H:
return maxFontSize(s,W,H,mid+1,right)
s="string"
size=maxFontSize(s,W,H,0,30)
if size==0:
print("'{s}' is too large for the dimensions of the rectangle".format(s=s))
else:
print("size {size} is the largest font size for the rectangle".format(size=size))
You can do a binary search for the maximum size, something like (in python3)
- rahulgurnani09 April 25, 2019