Encoding interview question

Preprocessing & Feature EngineeringEncodingLogistic Regressioneasy

A retailer's store_id has 1,000 levels. Instead of one-hot encoding, a colleague proposes binary encoding: give each store an integer code and write that code in base 2, one column per bit. How many columns does this create, and what is its main cost for a logistic regression?

  1. A10 columns, but with only 10 bits some stores must end up sharing a bit pattern, so their rows collide and become indistinguishable, just as with the hashing trick.
  2. B10 columns at no real cost: every store keeps its own unique bit pattern, so the logistic regression can still fit a separate effect for each of the 1,000 stores.
  3. C9 columns, because the codes 0 to 999 need ⌊log₂ 1,000⌋ = 9 bits; the cost is that trees can no longer isolate one store, while linear models are unaffected.
  4. D10 columns, since 2¹⁰ = 1,024 ≥ 1,000; unrelated stores share bit columns, so each bit's coefficient ties together arbitrary groups of stores in the model.
Show the answer and explanation

Correct answer: D, 10 columns, since 2¹⁰ = 1,024 ≥ 1,000; unrelated stores share bit columns, so each bit's coefficient ties together arbitrary groups of stores in the model.

Explanation Encoding

Binary encoding first maps each level to an integer code and then writes the code in base 2. With 1,000 levels you need ⌈log₂ 1,000⌉ = 10 bits, because 2⁹ = 512 is too few and 2¹⁰ = 1,024 is enough. So 10 columns replace roughly 1,000 one-hot columns.

The mapping is lossless: each store gets its own bit pattern, so nothing collides. That is the difference from the hashing trick, where unrelated levels can land in the same bucket.

The cost appears in a linear model. A logistic regression now has only 10 coefficients for store_id, one per bit, and a store's effect is the sum of the coefficients of its active bits. The bit worth 8 is on for an arbitrary mix of stores (codes 8–15, 24–31, …), so the model must give those unrelated stores a shared component of their effect. It cannot represent 1,000 independent store effects, and the similarities it imposes depend only on how the codes happened to be assigned.

EncodingColumns for 1,000 levelsSeparate effect per level in a linear model?
One-hot~1,000yes
Binary10no, bits are shared arbitrarily
Single integer code1no, and it implies an order

Tree models cope better, since a path of up to 10 splits can still isolate any single store.

Interview tip: present binary encoding as a compact middle ground for tree models when one-hot is too wide, and name target encoding or embeddings as the usual alternatives for linear and neural models.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Preprocessing & Feature Engineering questions

  1. Your features include age (years), income (TRY) and num_logins (count), on very different scales. For which group of models is feature scaling generally…
  2. A gradient-boosted house-price model for Istanbul uses raw latitude and longitude as features. Prices are highest around a few prime districts and along the…
  3. How do MCAR, MAR and MNAR differ in what the probability of a value being missing depends on?
  4. What does one-hot encoding do to a categorical feature such as color with the values red, green and blue?
  5. In model training and evaluation, what does the term data leakage mean?