Fractional Knapack as asked in an interview
float val = 0;
        while (queue.size() &gt; 0) {
            if (capacity == 0) return val;
            Pair max = queue.poll();
            float a = Math.min(max.weight, W);
            val += a * (max.value / max.weight);
            if (max.weight - a &gt; 0) // to prevent division by zero
                queue.add(new Pair(max.value, max.weight - a));
            capacity -= a;
        }
        return val;



Consider 

        float val = 0;

        while (!queue.isEmpty()) {
            Pair max = queue.poll();
            if (max.weight &lt; capacity) {
                val += max.value;
                capacity -= max.weight;
            } else {
                return val + capacity * (max.value / max.weight);
            }
        }

        return val;


The canonical way to check if a collection is empty is to call the isEmpty() method.  

We check that the capacity hasn't been reduced to zero before the end of the loop rather than at the beginning.  Note that a side effect of this is that we don't care about passing 0.0.  In the original code, floating point rounding could allow you to pass the equality check.  

Rather than take Math.min, we compare max.weight and capacity.  This simplifies the logic, as we don't have to multiply and divide by the same value in the expected case.  We only do that for the final item.