FastPrepBinary Linked List to Decimal

Binary Linked List to Decimal

JP Morgan Chase logoJP Morgan Chase● EasyINTERNOA

Updated

Problem statement

A linked list contains only bits 0 and 1, with the most significant bit at its head. Return the nonnegative decimal value represented by the list.

For this exercise, the runner supplies bits, an array containing the node values in head-to-tail order. Traverse them in that order. Leading zeros are allowed, and an empty list represents 0. This array is a wire representation of the list; node allocation and links are outside the callable contract.

Function

binaryListToDecimal(bits: int[]) → int

Examples

Example 1

bits = [1,0,1,1,0]return = 22

The bits represent 16+4+2=22, reading the list from head to tail.

Example 2

bits = []return = 0

An empty list has no set bits and represents 0.

Constraints

  • 0 <= bits.length <= 30
  • 0 <= bits[i] <= 1

More JP Morgan Chase Online Assessment questions

Similar questions at other companies

Interview reports that mention this question

JP Morgan Chase interview experiences

All JP Morgan Chase interview questionsSee JP Morgan Chase hiring insights
Saved
class Solution {
    public int binaryListToDecimal(int[] bits) {
        // Write your code here.
    }
}
Checking account…